2011-11-09 30 views
3

我有UserControl中的DependencyProperty问题。我的控件公开了两个Dependencyproperties,一个布尔和一个字符串。字符串属性起作用,但布尔没有。我没有得到任何的错误,但是改变并没有反映出来。UserControl中的DependencyProperty布尔

我定义属性是这样的:

private static readonly DependencyProperty IncludeSubdirectoriesProperty = 
    DependencyProperty.Register(
     "IncludeSubdirectories", 
     typeof(bool), 
     typeof(DirectorySelect), 
     new FrameworkPropertyMetadata(false) { BindsTwoWayByDefault = true } 
     ); 

public bool IncludeSubdirectories 
{ 
    get { return (bool) GetValue(IncludeSubdirectoriesProperty); } 
    set { SetValue(IncludeSubdirectoriesProperty, value); } 
} 

在XAML对于i结合属性这样的用户控制:

<CheckBox 
    Name="IncludeSubdirectoriesCheckbox" 
    IsChecked="{Binding Path=IncludeSubdirectories, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 
    Include subfolders</CheckBox> 

当我使用控制I结合到属性如下:

<Controls:DirectorySelect 
    Directory="{Binding Directory}" 
    IncludeSubdirectories="{Binding WatchSubDirs}"/> 

“目录”是字符串属性,工作得很好。我以同样的方式将他们绑定到他们身上,但我无法让布尔工作。

我哪里出错了?

+1

你怎么看该变化没有反映出来?你有没有在'WatchSubDirs'而不是'IncludeSubdirectoriesProperty'本身设置断点? 'WatchSubDirs' DP还是简单的属性? – sll

+0

VS在编译时不会发出信号,但在Visual Studio的Output窗口上打印日志信息。总是写些东西。在所有其他的事情中,你会发现绑定失败,转换失败或其他任何错误... – Tigran

+0

我不知道输出窗口中的状态消息。这有助于很多:)谢谢。 – SimonHL

回答

3

您可以尝试将绑定与用户控件绑定到一个元素绑定。在确定给你的userControl一个名字之前。

然后改变:

 IsChecked="{Binding Path=IncludeSubdirectories, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 

为了这样的事情:

 IsChecked="{Binding Path=IncludeSubdirectories, ElementName="<UserControlName>", Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 

另一种合理性检查,您可以执行是确保为IncludeSubdirectoriesProperty类型所有者是正确的。

+0

这样做。 问题是,我的用户控件中的复选框被绑定到datacontext窗口中我使用我的控件,而不是它自己的属性。错误的datacontext与我的usercontrol(Directory)具有相同名称的属性,所以绑定意外地使用该属性,但不包括IncludeSubdirectories。 感谢您的帮助。 – SimonHL

1

尝试这找出什么不顺心

private static readonly DependencyProperty IncludeSubdirectoriesProperty = 
    DependencyProperty.Register(
     "IncludeSubdirectories", 
     typeof(bool), 
     typeof(DirectorySelect), 
     new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnIncludeSubdirectoriesPropertyChanged)) { BindsTwoWayByDefault = true } 
     ); 

privatestaticvoid OnIncludeSubdirectoriesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { 
    // make a breakpoint here 
} 

调试绑定

<CheckBox Name="IncludeSubdirectoriesCheckbox" 
      IsChecked="{Binding Path=IncludeSubdirectories, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, diagnostics:PresentationTraceSources.TraceLevel=High}">Include subfolders</CheckBox> 

<Controls:DirectorySelect Directory="{Binding Directory}" IncludeSubdirectories="{Binding WatchSubDirs, diagnostics:PresentationTraceSources.TraceLevel=High}"/> 

必须包括

<Window xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase" /> 

也是在工具 - >选项 - > Debugging->输出窗口 改变WPF跟踪设置数据绑定=警告

现在看输出中的窗口会发生什么

希望这有助于

相关问题