SelectionChanged Event does not fire after reselecting Datagrid row?
Clash Royale CLAN TAG#URR8PPP
SelectionChanged Event does not fire after reselecting Datagrid row?
A simple Datagrid
which groups row by one of the property:
Datagrid
<DataGrid x:Name="MyDataGrid" SelectionChanged="MyDataGrid_SelectionChanged" ItemsSource="Binding Programs"
Style="StaticResource dataGridStyle">
<DataGrid.GroupStyle>
<GroupStyle ContainerStyle="StaticResource GroupHeaderStyle">
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="Binding Path=Heading" />
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
Style
if matters:
Style
<Style x:Key="GroupHeaderStyle" TargetType="x:Type GroupItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="x:Type GroupItem">
<StackPanel>
<TextBlock Text="Binding Name" FontWeight="Bold" Foreground="Aqua" HorizontalAlignment="Left" Margin="10,0,0,10" VerticalAlignment="Top"/>
<ItemsPresenter />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="dataGridStyle" TargetType="DataGrid">
<Setter Property="SelectionMode" Value="Extended"/>
<Setter Property="AutoGenerateColumns" Value="True"/>
<Setter Property="CanUserDeleteRows" Value="False"/>
<Setter Property="Background" Value="#302E2A"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="CanUserResizeColumns" Value="False"/>
<Setter Property="CanUserResizeRows" Value="False"/>
<Setter Property="CanUserSortColumns" Value="False"/>
<Setter Property="HeadersVisibility" Value="None"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Height" Value="Auto"/>
<Setter Property="Width" Value="Auto"/>
<Setter Property="VerticalScrollBarVisibility" Value="Visible"/>
<Setter Property="HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Margin" Value="10"/>
<Setter Property="GridLinesVisibility" Value="None"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="SelectionMode" Value="Single"/>
</Style>
Code behind:
private void FillSummit()
public ListCollectionView Programs get; private set;
Programs = new ListCollectionView(SummitPrograms);
Programs.GroupDescriptions.Add(new
PropertyGroupDescription("Room"));
private void MyDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
DataGrid dg = sender as DataGrid;
System.Windows.MessageBox.Show($"dg?.SelectedItem");
Question:
When i click on any row it pops up a message, after closing it when i again select the same row it doesnt raise selectionChangedEvent
So how can i make it work so it pops up again?
so is there any other event which make it work for reselection
– tabby
Aug 10 at 12:03
1 Answer
1
The SelectionChanged
event is only supposed to be raised when the selection actually changes to this is the expected behaviour.
SelectionChanged
You could try to handle the PreviewMouseLeftButtonDown
of the DataGridRow
containers:
PreviewMouseLeftButtonDown
DataGridRow
<DataGrid ...>
<DataGrid.ItemContainerStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="dg_MouseLeftButtonDown"></EventSetter>
</Style>
</DataGrid.ItemContainerStyle>
...
</DataGrid>
Thanks it works!
– tabby
Aug 10 at 12:14
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
The SelectionChanged event is only supposed to be raised when the selection actually changes to this is the expected behaviour.
– mm8
Aug 10 at 12:02