Jos*_*nel 5 c# wpf dependencies dependency-properties
我正在尝试将一个Dependency属性从我的UserControl绑定到我的MainViewModel.
这就是DependencyProperty的样子:
public static DependencyProperty ItemHasChangesProperty = DependencyProperty.Register("ItemHasChanges",
typeof(bool),
typeof(MyUserControl),
new PropertyMetadata(null));
public bool ItemHasChanges
{
get { return (bool)GetValue(ItemHasChangesProperty); }
set { SetValue(ItemHasChangesProperty, value); }
}
Run Code Online (Sandbox Code Playgroud)
我的XAML:
<local:MyUserControl ItemHasChanges="{Binding Path=Changes}" Grid.Row="4" />
Run Code Online (Sandbox Code Playgroud)
现在,在调试和检查Set-Accessor时bool Changes,我发现当我在UserControl中设置时它永远不会被访问ItemHasChanges = true;
知道我在这里做错了吗?
谢谢!
干杯
Jos*_*nel 10
明白了......我不得不改变
<local:MyUserControl ItemHasChanges="{Binding Path=Changes}" Grid.Row="4" />
Run Code Online (Sandbox Code Playgroud)
至
<local:MyUserControl ItemHasChanges="{Binding Path=Changes, Mode=OneWayToSource}" Grid.Row="4" />
Run Code Online (Sandbox Code Playgroud)
花了我3个小时来搞清楚..哈哈:-)
干杯