WPF 绑定返回错误的对象
我在 WPF 中有一个列表框,我将代码中的 ItemsSource 属性设置为“列表”列表。
当我现在运行该程序时,我得到一个带有我的类名的列表,其中包含与列表包含的条目一样多的条目。没错。
现在我指定以下数据模板:
<DataTemplate>
<NetworkEditor:NetworkEditor DisplayNetwork="{Binding}"></NetworkEditor:NetworkEditor>
</DataTemplate>
但对于 DependencyPropery,“DisplayNetwork”始终传递“null”(我使用 DebugValueConverter 对此进行了测试)。
有什么想法吗?
列表框的 Xaml:
<ListBox Name="myLst" Grid.Row="3" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" >
<ListBox.ItemTemplate>
<DataTemplate>
<NetworkEditor:NetworkEditor DisplayNetwork="{Binding}"></NetworkEditor:NetworkEditor>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我的 UserControl 中属性的实现:
public Network DisplayNetwork
{
get { return (Network)GetValue(DisplayNetworkProperty); }
set { SetValue(DisplayNetworkProperty, value); }
}
// Using a DependencyProperty as the backing store for DisplayNetwork. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DisplayNetworkProperty =
DependencyProperty.Register("DisplayNetwork", typeof(Network), typeof(NetworkEditor), new FrameworkPropertyMetadata(null, OnDisplayNetworkChanged, CoerceValueCallback));
private static Object CoerceValueCallback(DependencyObject d,Object baseValue)
{
return baseValue;
}
OnDisplayNetworkChanged 从未被调用,因为 null 始终设置为 Value!
我的列表框的数据源:
myLst.ItemsSource = ((S7FunctionBlock) myBlock).Networks;
其中网络是一个列表,当我调试此行时,它包含数据!
I have a ListBox in WPF where I set ItemsSource Property in Code to a List of "List"
When i now run the Program, i get a List with my classname with as much entrys as the List contains. Thats correct.
Now i secify the following Datatemplate:
<DataTemplate>
<NetworkEditor:NetworkEditor DisplayNetwork="{Binding}"></NetworkEditor:NetworkEditor>
</DataTemplate>
But to the DependencyPropery "DisplayNetwork" is always passed "null" (I tested this with a DebugValueConverter).
Any Ideas?
Xaml of the List Box:
<ListBox Name="myLst" Grid.Row="3" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" >
<ListBox.ItemTemplate>
<DataTemplate>
<NetworkEditor:NetworkEditor DisplayNetwork="{Binding}"></NetworkEditor:NetworkEditor>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Implementaion of the Property in my UserControl:
public Network DisplayNetwork
{
get { return (Network)GetValue(DisplayNetworkProperty); }
set { SetValue(DisplayNetworkProperty, value); }
}
// Using a DependencyProperty as the backing store for DisplayNetwork. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DisplayNetworkProperty =
DependencyProperty.Register("DisplayNetwork", typeof(Network), typeof(NetworkEditor), new FrameworkPropertyMetadata(null, OnDisplayNetworkChanged, CoerceValueCallback));
private static Object CoerceValueCallback(DependencyObject d,Object baseValue)
{
return baseValue;
}
OnDisplayNetworkChanged is never called, because null is always set as Value!
Data Source of my ListBox:
myLst.ItemsSource = ((S7FunctionBlock) myBlock).Networks;
where Networks is a List, and when I debug this Line, it contains data!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否检查过您的
NetworkEditor:NetworkEditor
类,是否在其中设置了 DataContext。从类内部设置 DataContext,然后尝试从此标记上的 Xaml 访问 DataContext 是一个经常犯的错误,认为父级 DataContext 将被返回。要检查这一点,请尝试将 XAML 更改为以下内容。
如果有错误请评论,我没有测试过。然而,正如 Will Dean 所说,在这种情况下,如果网络编辑器在您的控制之下,更改网络编辑器将是一个好主意。
Have you checked your
NetworkEditor:NetworkEditor
class, if you set the DataContext there in. It is an often made mistake to set the DataContext from within a class and then to try accessing the DataContext from Xaml on this tag, thinking that the parents DataContext will be returned.For checking this, try to change your XAML to the following.
Make a comment if there is an error in, I have not tested it. However, as also Will Dean says, in this case changing the NetworkEditor would be a good idea, if it is under your control.
正如 HCL 所说,该控件的 DataContext 可能不是您想象的那样。
要诊断此问题,您可以将 {Binding} 更改为 {Binding SomethingThatDoesntExist},然后在 VS 中打开绑定警告。绑定警告消息将告诉您检查了哪种类型的对象的“SomethingThatDoesntExist” - 您可能会发现它不是您所期望的。
如果事实证明 NetworkEditor 将其 DataContext 设置为与您想象的不同的内容,那么解决此问题的一个好方法(假设 NetworkEditor 在您的控制之下)是更改为在 NetworkEditor 中的第一个对象上设置 DataContext(通常是典型 UserControl 中的网格,而不是 NetworkEditor 对象本身。
As HCL says, the DataContext of that control is probably not what you think it is.
To diagnose this, you could change {Binding} to {Binding SomethingThatDoesntExist}, then turn on binding warnings in VS. The binding warning message will tell you which type of object was checked for 'SomethingThatDoesntExist' - you'll probably find it's not what you expected.
If it does turn out to be that NetworkEditor is setting its DataContext to something different to what you think, then a good solution to this problem (provided NetworkEditor is under your control), is to change to setting DataContext on the first object within NetworkEditor (often a Grid in a typical UserControl), rather than on the NetworkEditor object itself.