重新选择Datagrid行后,SelectionChanged事件不触发? [英] SelectionChanged Event does not fire after reselecting Datagrid row?

查看:46
本文介绍了重新选择Datagrid行后,SelectionChanged事件不触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的 Datagrid 按属性之一分组:

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

后面的代码:

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}");
} 

问题:
当我单击在任何行上,它都会弹出一条消息,在关闭该行后,当我再次选择同一行时,它不会引发selectionChangedEvent
那么我如何使它起作用,以便它再次弹出?

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?

推荐答案

仅当选择实际上更改为预期的行为时,才引发 SelectionChanged 事件。

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

您可以尝试处理 DataGridRow PreviewMouseLeftButtonDown 容器:

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>

这篇关于重新选择Datagrid行后,SelectionChanged事件不触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆