绑定到子项属性

鲁斯兰·F。

我有他们的视图模型页面。将APage替换为Frameusing中的Frame.NavigationManager.Navigate()

在一个Page我有GroupBox一个孩子DataGrid我要根据中的项目数GroupBox更改它VisibilityDataGrid

这是我所拥有的:

<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:Path = 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");
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章