SelectionChanged Event does not fire after reselecting Datagrid row?

只愿长相守 提交于 2019-12-24 08:07:14

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/51786072/selectionchanged-event-does-not-fire-after-reselecting-datagrid-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!