我对.NET4附带的新DataGrid组件有一个问题。使用RowDetails时出现问题。使用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的蓝色背景正在显示。
有什么办法可以解决这个问题吗?我假设这是组件中的错误。如果没有解决方案,有人能告诉我在哪里报告错误吗?
发布于 2010-04-23 03:24:46
当DataGrid在WPF Toolkit中时,我也遇到了同样的问题,当时也找不到解决方案。如果有人对我有解决方案--请大声喊出来!但我猜这是组件中的错误,并提交了一个bug report to Microsoft。
发布于 2013-02-28 15:44:04
答案在这里:WPF DataGrid Row not adjusting height after expanding Row Details
你可以在你的DataGrid风格中使用next setter:
<Setter Property="ScrollViewer.CanContentScroll" Value="False" />我觉得这是某种魔法。
https://stackoverflow.com/questions/2690977
复制相似问题