首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与RowDetails一起使用时,WPF DataGrid的高度错误

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

Stack Overflow用户
提问于 2010-04-22 21:04:28
回答 2查看 1.3K关注 0票数 2

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

考虑一下这个简单的DataGrid:

代码语言:javascript
复制
<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>

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

代码语言:javascript
复制
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的蓝色背景正在显示。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-04-23 03:24:46

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

票数 3
EN

Stack Overflow用户

发布于 2013-02-28 15:44:04

答案在这里:WPF DataGrid Row not adjusting height after expanding Row Details

你可以在你的DataGrid风格中使用next setter:

代码语言:javascript
复制
<Setter Property="ScrollViewer.CanContentScroll" Value="False" />

我觉得这是某种魔法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2690977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档