[英]Binding to child item property
我有他们的视图模型的页面。 甲Page
是在displaed Frame
使用Frame.NavigationManager.Navigate()
在一个Page
我有一个带有子DataGrid
的GroupBox
。 我希望GroupBox
根据DataGrid
项目数来更改它的Visibility
。
这是我有的:
<GroupBox ....
Visibility="{Binding ElementName=SomeDataGrid,
Path=HasItems,
Converter={StaticResource BooleanToVisibilityConverter}}">
<DataGrid x:Name="SomeDataGrid"
IsReadOnly="True"
ItemsSource="{Binding Items}"/>
</GroupBox>
将Page
更改为另一个并返回后,我有以下绑定异常
System.Windows.Data错误:4:无法找到与引用绑定的源
'的ElementName = SomeDataGrid'。 BindingExpression:路径= HasItems;
我尝试过使用x:Reference
但遇到了同样的问题。
有人可以解释我做错了吗?
可能, Items
集合在某些时候是空的,这使得GroupBox
变得崩溃。 当GroupBox
折叠时,它会从视图中删除其内容( DataGrid
)。
从视图中删除DataGrid
后, Binding
无法再找到它的引用,因此它会中断。
如果我是你,我会将GroupBox
Visibility
直接绑定到ViewModel属性,而不是将其绑定到DataGrid
。
<GroupBox ....
Visibility="{Binding HasItems,
Converter={StaticResource BooleanToVisibilityConverter}}">
<DataGrid x:Name="SomeDataGrid"
IsReadOnly="True"
ItemsSource="{Binding Items}"/>
</GroupBox>
在ViewModel中:
public bool HasItems
{
get
{
return Items != null && Items.Count() > 0;
}
}
public IEnumerable Items
{
get
{
// ...
}
set
{
// ...
RaisePropertyChanged("HasItems");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.