C#/ WPF:依赖项属性没有更新绑定属性?
问题描述:
我想从我的UserControl绑定一个依赖属性到我的MainViewModel。C#/ WPF:依赖项属性没有更新绑定属性?
这是的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); }
}
我的XAML:
<local:MyUserControl ItemHasChanges="{Binding Path=Changes}" Grid.Row="4" />
现在,当调试和检查“布尔变化”的set访问,我看到它永远不会当我在UserControl中设置ItemHasChanges = true时访问;
任何想法我在这里做错了吗?
谢谢!
干杯
答
明白了。我不得不改变
<local:MyUserControl ItemHasChanges="{Binding Path=Changes}" Grid.Row="4" />
到
<local:MyUserControl ItemHasChanges="{Binding Path=Changes, Mode=OneWayToSource}" Grid.Row="4" />
我花了约3小时的数字出来..哈哈:-)
干杯
答
你直接控制设置ItemHasChanges
(如,不通过更新绑定源)?如果是这样,那将删除绑定。
+0
无我将源代码绑定到控件,并根据一个操作,我更改了源的值.. – 2009-12-14 15:01:25
我浪费了1小时(: – Mel 2012-10-21 09:22:34
对于阅读此内容的其他人员,在Universal Windows Apps中,没有“OneWayToSource”模式。相反,我使用了'OneWay'。 – 2016-02-26 16:41:22
'TwoWay'也适用,如果你需要双向更新。这也浪费了我大约一个小时的时间,认为我的财产没有定下它的价值。 – Dan 2017-09-07 15:32:40