与 RowDetails 一起使用时,WPF DataGrid 的高度错误

发布于 2024-08-30 01:18:31 字数 1921 浏览 1 评论 0原文

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

考虑这个简单的 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 的蓝色背景正在显示。

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

I have a problem with the new DataGrid component that comes with .NET4. The problem occurs when using RowDetails. With RowDetails the total height of the grid is increased when an element is chosen. This is necessary to show all Rows and the RowDetails, and exactly what I expect. When choosing another row the first RowDetails will collapse and the details for the newly selected row is expanded. The problem now is that the total height of the DataGrid seems to include the collapsed rowdetails of the previous element. My guess is that it first opens the new row details, and then collapses the old - and never resize to smaller sizes.

Consider this simple 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>

It also requires a few lines in the codebehind:

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

Select one element, then another one - and you will see that the blue background of the DataGrid is showing.

Is there any way I can fix this problem? I'm assuming this is a bug in the component. If there is no solution; can someone tell me where to report the bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日记撕了你也走了 2024-09-06 01:18:31

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

I had the same issue when the DataGrid was in the WPF Toolkit, and couldn't find a solution then either. If anyone has a solution to me - please shout out! But I'm guessing this is a bug in the component, and filed a bug report to Microsoft.

极致的悲 2024-09-06 01:18:31

答案在这里: WPF DataGrid Row 扩展后不调整高度行详细信息

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

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

我认为这是某种魔法。

Answer is here: WPF DataGrid Row not adjusting height after expanding Row Details

You can use next setter in your DataGrid Style:

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

I think it's some kind of magic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文