1

WPF DataGrid has a property that allows virtualization to work with grouping: VirtualizationPanel.IsVirtualizingWhenGrouping="True". When I set it to True, I notice that columns that have star sizing, are no longer resized when the entire data grid width changes.

To reconstruct, create a new WPF app, and set the following code in MainWindow.xaml:

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d">
<Grid>
    <DataGrid x:Name="grid" 
            CanUserResizeColumns="False"                
            AutoGenerateColumns="False"
            VirtualizingPanel.IsVirtualizingWhenGrouping="True"
            >
        <DataGrid.GroupStyle>
            <GroupStyle/>
        </DataGrid.GroupStyle>
        <DataGrid.Columns>
            <DataGridTextColumn Width="100" Header="1" Binding="{Binding .}">
            </DataGridTextColumn>
            <DataGridTextColumn Width="*" Header="2" Binding="{Binding .}">
            </DataGridTextColumn>
            <DataGridTextColumn Width="100" Header="3" Binding="{Binding .}">
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

And the following in the MainWindow.xaml.cs:

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        var csv = new CollectionViewSource();
        csv.Source = new List<string>
        {
            "John", 
            "Paul", 
            "George", 
            "Ringo",
            "John",
            "Paul",
            "George",
            "Ringo",
        };

        csv.GroupDescriptions.Add(new PropertyGroupDescription("."));

        grid.ItemsSource = csv.View;
    }
}

Notice that when you resize the window, the middle column is not resized. But if you set VirtualizationPAnel.IsVirtualizingWhenGrouping="False" resizing will work as expected

Kobi Hari
  • 1,259
  • 1
  • 10
  • 25
  • Hello. I've added an answer below. Also tested with your code and it worked as you need. Could you accept the answer if it is useful? – Bulutay Saraç Oct 01 '19 at 18:56

2 Answers2

5
<DataGrid.GroupStyle>
    <GroupStyle>
        <GroupStyle.Panel>
            <ItemsPanelTemplate>
                <DataGridRowsPresenter/>
            </ItemsPanelTemplate>
         </GroupStyle.Panel>
    </GroupStyle>
</DataGrid.GroupStyle>

Will solve your problem.

2

I guess, GroupStyle's default PanelTemplate was breaking your design. Try this. (I've tested with your xaml codes, it worked.)

I have found this soluiton from here : https://stackoverflow.com/a/7025995/4991973

Bulutay Saraç
  • 644
  • 9
  • 20
  • 1
    I can confirm, that in a similar constellation with both, virtualisation and dynamic columns, this fixed my Issue. The solution provided, overrides the defaul template with a very rudimentary `ItemsPanelTemplate`, that only consists of a bare `DataGridRowsPresenter`. You might want to add some `ContainerStyle` because its only a white box now. – LuckyLikey Mar 27 '20 at 14:55