【发布时间】:2012-01-30 03:29:18
【问题描述】:
我有一个 UserControl,它用作 ItemsControl 中项目的基础:
主页 xaml:
<ItemsControl ItemsSource="{Binding Systems, Mode=TwoWay}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:ServerGroupControl>
<local:ServerGroupControl.DataContext>
<local:ServerGroupControlViewModel System="{Binding}"/>
</local:ServerGroupControl.DataContext>
</local:ServerGroupControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我正在尝试设置每个 ViewModel 的“系统”属性(以便它可以处理视图的数据),但该属性从未设置!
这是视图模型类中的依赖属性声明:
public static readonly DependencyProperty SystemProperty = DependencyProperty.Register(
"System",
typeof(ServerGroup),
typeof(ServerGroupControlViewModel)
);
public ServerGroup System
{
get { return (ServerGroup)GetValue(SystemProperty); }
set { SetValue(SystemProperty, value); }
}
该属性始终保持其默认值。关于为什么此设置不起作用的任何想法?
【问题讨论】:
-
哪个疯子在虚拟机中有 DP?
-
另外,您是如何确定该属性从未设置的?如果您在该设置器中设置断点,您将永远感到失望,因为绑定系统不使用它们。
-
@H.B 我稍后会检查视图(中断使用该属性的命令的处理程序)。我确实已经阅读过如何根据 wpf 的魔法跳过设置器。
-
好吧,另一个未知数:绑定真的有效没有抛出binding errrors吗?这就是我在这里的怀疑。
-
是的,除非我尝试 Mode=TwoWay (正如先前消失的答案所建议的那样),否则不会引发绑定错误。 Mode=TwoWay 的错误指定必须设置“Path or XPath”,即不能双向绑定到 DataTemplate 的 DataContext,这是有道理的。