简体   繁体   中英

SelectionChanged Event does not fire after reselecting Datagrid row?

A simple Datagrid which groups row by one of the property:

<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 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?

The SelectionChanged event is only supposed to be raised when the selection actually changes to this is the expected behaviour.

You could try to handle the PreviewMouseLeftButtonDown of the DataGridRow containers:

<DataGrid ...>
    <DataGrid.ItemContainerStyle>
        <Style TargetType="DataGridRow">
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="dg_MouseLeftButtonDown"></EventSetter>
        </Style>
    </DataGrid.ItemContainerStyle>
    ...
</DataGrid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM