【问题标题】:Wpf DataGrid issueWpf DataGrid 问题
【发布时间】:2011-09-14 12:02:24
【问题描述】:

要重现此问题,请添加用户控件,粘贴到下面的 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; }

}

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

谢谢

【问题讨论】:

  • 这可能无法解决您的问题,但过去我在使用列表时遇到了麻烦,也许尝试将列表更改为 ObservableCollection?
  • 也是一个很好的观点。谢谢:)

标签: wpf wpfdatagrid


【解决方案1】:

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

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

<DataGrid Loaded="dataGrid1_Loaded"
          ...>

事件处理程序:注意 SelectedIndex 为 0

private void dataGrid1_Loaded(object sender, RoutedEventArgs e)
{
    DataGrid dataGrid = sender as DataGrid;
    dataGrid.SelectedItem = null;
}

【讨论】:

  • 这为我解决了问题。奇怪——我没想到会这样,因为如果我对 KittensView 进行不同的排序,我会看到“Alfie”出现同样的问题,即使它在网格中排在最后!
  • 很好奇。仅在更改事件上“绘制”或评估它似乎很奇怪。我的意思是它不是,这正是我所期望的,如果这又回到了 winforms 领域,但我期待更多的魔法 :) 谢谢你,Meleak,周末的快乐奖金 :)
  • 我想我现在已经把赏金给你了,对不起,以前没有用过它们:) 谢谢你的帮助!
  • 谢谢!由于项目是通过 mvvm 加载的,因此 Loaded 事件不符合我的需要,因此我将此解决方法放在 SelectionChanged 事件中:将我的代码放在单独的答案中以提高可读性
【解决方案2】:

非常感谢@Fredrik 这是我评论后的代码:

XAML:

<DataGrid SelectionChanged="DataGrid_SelectionChanged">

codebehind.cs:

private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DataGrid dataGrid = sender as DataGrid;
    dataGrid.SelectionChanged -= DataGrid_SelectionChanged;
    dataGrid.SelectedItem = null;
    dataGrid.SelectionChanged += DataGrid_SelectionChanged;
}

此代码还允许根据需要刷新数据

【讨论】:

    猜你喜欢
    • 2013-04-21
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多