Domanda

I have 3 datagrids. Second and third datagrid should show only subset of values from the first datagrid based on the selected row on the first datagrid.

C#:

_context.MyFor.Load();
F = new ObservableCollection<DB.Forum>(_context.MyFor.Local);

In XAML I define the resources:

<Window.Resources>
    <CollectionViewSource Source="{Binding F}" x:Key="CVSF" x:Name="CVSF" />
    <CollectionViewSource Source="{Binding FUsers, Source={StaticResource CVSF}}" x:Key="CVSUsers" x:Name="CVSUsers" />
    <CollectionViewSource Source="{Binding FOptions, Source={StaticResource CVSF}}" x:Key="CVSOptions" x:Name="CVSOptions" />
</Window.Resources>

The binding is done this way:

// Datagrid 1:
<DataGrid ItemsSource="{Binding Source={StaticResource CVSF}}" />

// Datagrid 2:
<DataGrid ItemsSource="{Binding Source={StaticResource CVSUsers}}">
   <DataGrid.Columns>
      <DataGridTextColumn Binding="{Binding User.UserUsername}" Header="UserName" x:Name="UserCol2" />
   </DataGrid.Columns>
</DataGrid>

// Datagrid 3:
<DataGrid ItemsSource="{Binding Source={StaticResource CVSOptions}}" PresentationTraceSources.TraceLevel="High" >
   <DataGrid.Columns>
      <DataGridTextColumn Binding="{Binding CountryName}" Header="Country1" />
      <DataGridTextColumn Binding="{Binding Country.CountryName}" Header="Country2" />
      <DataGridTextColumn Binding="{Binding ForumOptions.Country.CountryName}" Header="Country3" />
   </DataGrid.Columns>
</DataGrid>

Problem: first datagrid shows everything. Second shows only those users that belong to the selection of fist datagrid (as expected). But third datagrid shows nothing (see error below). I guess it is because the return is an Object (not a CollectionView).

System.Windows.Data Error: 5 : Value produced by BindingExpression 
is not valid for target property.;
Value='System.Data.Entity.DynamicProxies.ForumOptions_0DF21D7'
BindingExpression:Path=ForumOptions; 
DataItem='ListCollectionView' (HashCode=34116599); 
target element is 'CollectionViewSource' (HashCode=62307935); 
target property is 'Source' (type 'Object')

How to get related results display on the third datagrid?

È stato utile?

Soluzione

Finally managed to find the sollution. Instead of binding to separate CollectionViewSource, I'd named the first datagrid as x:Name="FList" and then used binding to ElementName=FList, Path=SelectedItem and then defined the Path to the needed property.

<StackPanel DataContext="{Binding ElementName=FList, Path=SelectedItem, Mode=TwoWay}" Name="StackPanel2" >
    <TextBox Text="{Binding Path=ForumOptions.Country.CountryName}"/>
</StackPanel>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top