Reputation: 962
I have a DataGrid
which holds a number of objects. Within that DataGrid, I have a RowDetailsTemplate
to house all of the hierarchical data. I want to be able to select the object from within that DataGrid, but so far haven't had any luck with the following code
<DataGrid x:Name="leagueList" Grid.Row="1" ItemsSource="{Binding Leagues}" SelectedItem="{Binding SelectedLeague}"
Margin="0,0,5,0" ColumnWidth="1*"
CanUserAddRows="False" AutoGenerateColumns="False" IsReadOnly="True" CanUserReorderColumns="False" CanUserSortColumns="False">
<DataGrid.Resources>
<utility:SubtractValueConverter x:Key="SubtractValueConverter"/>
</DataGrid.Resources>
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<Expander Expanded="Expander_Expanded" Collapsed="Expander_Collapsed"/>
</DataTemplate>
</DataGrid.RowHeaderTemplate>
<DataGrid.Columns>
<DataGridTextColumn Header="League" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Initials" Binding="{Binding Initials}"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate DataType="{x:Type models:Team}">
<DataGrid ItemsSource="{Binding Teams}" SelectedItem="{Binding Path=SelectedTeam, UpdateSourceTrigger=PropertyChanged}"
CanUserAddRows="False" AutoGenerateColumns="False" IsReadOnly="True" GridLinesVisibility="All"
Width="{Binding ActualWidth,ElementName=leagueList, Converter={StaticResource SubtractValueConverter}, ConverterParameter=50}" HorizontalAlignment="Stretch" ColumnWidth="*" Margin="1,1,1,1" BorderThickness="2">
<DataGrid.Columns>
<DataGridTextColumn Header="Nickname" Binding="{Binding Nickname}"/>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
The SelectedTeam
Property is what I want to be called when a Team
is selected, but it doesn't appear to be bound to that property, even though I'm specifying it as such.
public Team SelectedTeam
{
get => _SelectedTeam;
set
{
_SelectedTeam = value;
System.Windows.MessageBox.Show("Selected Team is: " + _SelectedTeam.Nickname);
RaisePropertyChangedEvent("SelectedTeam");
}
}
When I run my app, I don't even get the MessageBox
popup when I select a Team, but I should.
The entire grid populates properly, I just can't get the SelectedItem from the inner DataGrid.
Upvotes: 1
Views: 217
Reputation: 962
After using both Jan's comment link and Satish's answers, I've found the solution.
The child DataGrid
needs to find the DataContext of it's parent, so using RelativeResource
to point to the UserControl
type will work, followed by inserting DataContext
in front of my property type. So the SelectedItem
line of code appears as follows:
SelectedItem="{Binding Path=DataContext.SelectedTeam, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
Upvotes: 1
Reputation: 1973
Since it is inside the template, it is difficult to bind the selected item, what you can do is use RelativeSource, below is the syntax & example.
SelectedItem = {Binding Path=SelectedTeam, RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}
typeOfAncestor :- Type of Ancestor is the type of class for which you have provided the namespace in the Declaration of Xaml, which the namespace of the viewmodel.
For example:
xmlns:local="clr-namespace:WpfApp2"
which needs to be at the top of the xaml page
SelectedItem = {Binding Path=SelectedTeam, RelativeSource={RelativeSource AncestorType={x:Type local:ViemModelName}}}
Relative Source relates or points towards the property. Now what happens is the SelectedTeam is binded from the View Model to this Selected Item Property.
Hope this helps you. Thanks,
Upvotes: 0