Child binding

I have pages with their view models. A Pageshifts to Frameusing Frame.NavigationManager.Navigate().

In one PageI have GroupBoxwith a child DataGrid. I want to GroupBoxchange it Visibilityaccording to the number of elements in DataGrid.

Here is what I have:

<GroupBox ....
          Visibility="{Binding ElementName=SomeDataGrid,
                                   Path=HasItems,
                                   Converter={StaticResource BooleanToVisibilityConverter}}">
        <DataGrid x:Name="SomeDataGrid"
                  IsReadOnly="True"
                  ItemsSource="{Binding Items}"/>
</GroupBox>

Problem

After changing Pageto another and going back, I have the following binding exception

System.Windows.Data error: 4: cannot find source for binding with reference

'ElementName = SomeDataGrid. BindingExpression: Path = HasItems;

I tried using x:Referencebut got the same problem.

Can someone explain what I'm doing wrong?

+4
1

, 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");
    }
}
+2

Source: https://habr.com/ru/post/1608292/


All Articles