重新选择Datagrid行后,SelectionChanged事件不触发?

时间:2018-08-10 12:01:15

标签: c# wpf datagrid

一个简单的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,如果有问题:

 <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 那么,如何使它工作以使其再次弹出?

1 个答案:

答案 0 :(得分:2)

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

您可以尝试处理PreviewMouseLeftButtonDown个容器中的DataGridRow

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