2

我对 .NET4 附带的新 DataGrid 组件有疑问。使用 RowDetails 时会出现问题。使用RowDetails,选择元件时,网格的总高度增加。这是显示所有行和行详细信息所必需的,并且正是我所期望的。选择另一行时,第一个 RowDetails 将折叠,新选择的行的详细信息将展开。现在的问题是 DataGrid 的总高度似乎包括前一个元素的折叠行详细信息。我的猜测是它首先打开新的行细节,然后折叠旧的 - 并且永远不会调整到更小的尺寸。

考虑这个简单的 DataGrid:

<DataGrid ItemsSource="{Binding Cars}" Background="Blue" SelectionMode="Single" AutoGenerateColumns="True" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,20,0,0" Width="450">
    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <TextBlock>Presenting the car details:</TextBlock>
                <TextBlock Text="{Binding Brand}"></TextBlock>
                <TextBlock Text="{Binding CarColor}"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>
</DataGrid>

它还需要代码隐藏中的几行:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new MyViewModel(); 
    }
}

public class MyViewModel
{
    private readonly ObservableCollection<Car> _cars = new ObservableCollection<Car>();

    public MyViewModel()
    {
        _cars.Add(new Car("Toyota", "Silver"));
        _cars.Add(new Car("VW", "Black"));
        _cars.Add(new Car("Audi", "Blue"));
    }

    public ObservableCollection<Car> Cars
    {
        get
        {
            return _cars; 
        }
    }
}

public class Car
{
    public Car(string brand, string color)
    {
        Brand = brand;
        CarColor = color; 
    }

    public string Brand { get; set; }
    public string CarColor { get; set; }
}

选择一个元素,然后选择另一个元素 - 您将看到 DataGrid 的蓝色背景正在显示。

有什么办法可以解决这个问题吗?我假设这是组件中的错误。如果没有解决方案;有人可以告诉我在哪里报告错误吗?

4

2 回答 2

3

当 DataGrid 在 WPF Toolkit 中时,我遇到了同样的问题,然后也找不到解决方案。如果有人对我有解决方案 - 请大声说出来!但我猜这是组件中的错误,并向 Microsoft 提交了错误报告

于 2010-04-22T19:24:46.173 回答
0

答案在这里:WPF DataGrid Row not adjust height after expand Row Details

您可以在 DataGrid 样式中使用下一个设置器:

<Setter Property="ScrollViewer.CanContentScroll" Value="False" />

我认为这是某种魔法。

于 2013-02-28T07:44:04.227 回答