简体   繁体   English

行折叠时,DataGrid不会调整大小

[英]DataGrid doesn't resize when rows are collapsed

I have a datagrid with rows that appear or disappear depending on some user preferences. 我有一个数据网格,其中包含根据某些用户首选项出现或消失的行。 This datagrid is part of a stackpanel that contains a few of these grids. 该数据网格是包含其中一些网格的堆栈面板的一部分。 The problem is that when the user hides some rows, the datagrid height doesn't shrink. 问题在于,当用户隐藏一些行时,数据网格高度不会缩小。 This only happens on windows xp and not on windows 7 where the grid shrinks as expected. 这仅在Windows XP上发生,而不在网格按预期缩小的Windows 7上发生。

Here is a small example that shows the issue: 这是一个显示问题的小示例:

The xaml: XAML:

<Window x:Class="TestDataGridResize.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
    <StackPanel Orientation="Vertical">
        <Button Content="Hide some rows" Click="Button_Click"/>
        <DataGrid Name="dataGrid" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn IsReadOnly="True" Binding="{Binding Name}" Header="Name" />
            </DataGrid.Columns>
            <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <Setter Property="Visibility" Value="{Binding Visibility}"/>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>
        <TextBlock Text="THE END" Background="AliceBlue" TextAlignment="Center"/>
    </StackPanel>

</Grid>

The code behind it: 其背后的代码:

public partial class MainWindow : Window
{
    private ObservableCollection<Element> elements = new ObservableCollection<Element>();

    public MainWindow()
    {
        InitializeComponent();

        this.dataGrid.ItemsSource = this.elements;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        for (int i = 0; i < 5; i++)
            this.elements[i].Visibility = System.Windows.Visibility.Collapsed;
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        for (int i = 0; i < 10; i++)
            this.elements.Add(new Element("Element " + i));
    }
}

public class Element : INotifyPropertyChanged
{
    private static PropertyChangedEventArgs VisibilityChanged = new PropertyChangedEventArgs("Visibility");

    private string name;
    public string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }

    private Visibility visibility;
    public Visibility Visibility
    {
        get { return this.visibility; }
        set
        {
            this.visibility = value;
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, VisibilityChanged);
        }
    }

    public Element(string name)
    {
        this.name = name;
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

The target framework is 4.0. 目标框架是4.0。 Any help would be much appreciated. 任何帮助将非常感激。

Try using the Grid to arrange the controls instead of the StackPanel : 尝试使用Grid而不是StackPanel来排列控件:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Button Grid.Row="0" Content="Hide some rows" Click="Button_Click"/>
    <DataGrid Grid.Row="1" Name="dataGrid" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn IsReadOnly="True" Binding="{Binding Name}" Header="Name" />
        </DataGrid.Columns>
        <DataGrid.RowStyle>
            <Style TargetType="{x:Type DataGridRow}">
                <Setter Property="Visibility" Value="{Binding Visibility}"/>
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>
    <TextBlock Grid.Row="2" Text="THE END" Background="AliceBlue" TextAlignment="Center"/>
</Grid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM