WPF DataGrid gets wrong height when used with RowDetails

I have a problem with the new DataGrid component that ships with .NET4. The problem occurs when using RowDetails. With RowDetails, the overall height of the grid increases as the item is selected. This is needed to show all Rows and RowDetails, and exactly what I expect. When another row is selected, the first RowDetails will fall apart and the details for the newly selected row will expand. Now the problem is that the total height of the DataGrid seems to include the collapsed row data of the previous item. My guess is that it first opens the new line details and then destroys the old one - and never resizes 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 of code:

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 item, then another, and you will see a blue DataGrid background displayed.

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

+2


a source to share


2 answers


I had the same problem when the DataGrid was in WPF Toolkit and couldn't find a solution. If anyone has a solution for me please shout out! But I am guessing this is a bug in the component and has filed a bug with Microsoft .



+3


a source


Answer here: WPF DataGrid Row does not adjust height after expanding growth details

You can use the following setter in your DataGrid style:



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

      

I think this is some kind of magic.

0


a source







All Articles