Wpf DataGrid 问题

标签 wpf wpfdatagrid

要重现此问题,请添加用户控件,粘贴下面的 xaml,然后将实例添加到窗口。最后将窗口的 datacontext 设置为 ADummyDataContext 的一个实例(也在下面)

当您第一次运行应用程序时,您应该得到一个包含三个类别的网格,每个类别包含一只猫。如果您单击底部两个类别中的任何一个并单击猫的名字,则会出现一个蓝色行,仅显示猫的名字。

但是,如果您单击第一行并单击猫的行,则不会出现蓝色行。
注意 :这只会在您第一次运行应用程序时发生。只要您单击任何其他猫,第一类中的猫就会按预期工作。

<UserControl x:Class="WpfUserControls.SimpleGridControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Background="#FFE46400">
<Grid Margin="2,2,2,2">
    <Grid.RowDefinitions>
        <RowDefinition Height="26" MaxHeight="26" MinHeight="26" />
        <RowDefinition />
        <RowDefinition Height="26" MaxHeight="26" MinHeight="26" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <ToolBar Grid.Row="0">
        <Button Content="Button" Name="button1" VerticalAlignment="Center" Width="75" />
        <Button Content="Button" Name="button2" VerticalAlignment="Center" Width="75" />
    </ToolBar>
    <DataGrid CanUserAddRows="False" ItemsSource="{Binding Path=KittensView}"  AutoGenerateColumns="True" Grid.Row="1"  HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Stretch">
        <DataGrid.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Path=Name}" />
                        </StackPanel>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander>
                                        <Expander.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding Path=Name}" Margin="0,0,5,0"/>
                                                <TextBlock Text="{Binding Path=ItemCount}"/>
                                                <TextBlock Text=" Items"/>
                                            </StackPanel>
                                        </Expander.Header>
                                        <ItemsPresenter />
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </DataGrid.GroupStyle>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <StackPanel Background="LightBlue" Orientation="Horizontal" >
                    <!-- <Image Height="32" Width="32" Source="/WpfUserControls;component/cat.png"></Image> -->
                    <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Height ="20" Text="{Binding Path=Name}"/>
                </StackPanel>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
    </DataGrid>
    <StatusBar Grid.Row="2"></StatusBar>

</Grid>
</UserControl>

这是数据上下文类和小猫类。
    public class ADummyDataContext
{
    public List<Kitten> Kittens { get; set; } 

    public ADummyDataContext()
    {
        Kittens = new List<Kitten>
                      {
                          new Kitten {Color = "Orange", Name = "Alfie", Weight=6, Sex="Male"},
                          new Kitten {Color = "Black and White", Name = "Smudge", Weight = 4, Sex="Female"},
                          new Kitten {Color = "Grey", Name = "Charlotte", Weight = 5, Sex="Female"}
                      };
        KittensView = new ListCollectionView(Kittens);
        KittensView.GroupDescriptions.Add(new PropertyGroupDescription("Weight"));
    }

    public ListCollectionView KittensView { get; set; }
}

public class Kitten
{
    public string Name { get; set; }
    public string Color { get; set; }
    public int Weight { get; set; }
    public string Sex { get; set; }

}

我特别想知道您是如何找出问题所在的。

谢谢

最佳答案

问题是DataGrid中的第一项首次加载时已被选中。但是,它并没有真正被选中,它没有显示为选中状态,并且组没有展开。但是当你第一次点击第一项时,DataGrid由于 SelectedIndex 无法分辨差异已经是 0。这真的很烦人,我之前几次注意到类似的行为。

作为解决方法,您可以取消选择 Loaded 中的第一项。 DataGrid事件

<DataGrid Loaded="dataGrid1_Loaded"
          ...>

事件处理程序:注意 SelectedIndex 为 0
private void dataGrid1_Loaded(object sender, RoutedEventArgs e)
{
    DataGrid dataGrid = sender as DataGrid;
    dataGrid.SelectedItem = null;
}

关于Wpf DataGrid 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7415988/

相关文章:

c# - 如何在 WPF 工具包数据网格控件中设置水平网格线的粗细?

WPF DataGridTemplateColumn 单元格样式不显示值

c# - 尝试更改 DataGrid 中的单元格颜色时出现 WPF 表单中的 InvalidOperationException

wpf:DataGrid 禁用选定的行样式 - 或行选择

c# - 只读依赖属性更新但首次使用时不起作用

wpf - 如何使用 DataGridCell 对象获取行索引

c# - 无法通过 Entity Framework 从 WPF 数据网格更新数据库

c# - ObservableCollection<T> 快速填充但缓慢绑定(bind)可视化

c# - 如何在 Windows 10 中打开显示设置(语法上,特别是使用 C#)?

c# - Popup 中的 WPF ListBox 在 PopupClose 上设置 null SelectedItem