views:

134

answers:

1

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?

+2  A: 

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.

stiank81
Microsoft has accepted it as a bug, so guess there is no solution to this before they fix it then..
stiank81