Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGrid没有';折叠行时不调整大小_C#_Wpf_Datagrid - Fatal编程技术网

C# DataGrid没有';折叠行时不调整大小

C# DataGrid没有';折叠行时不调整大小,c#,wpf,datagrid,C#,Wpf,Datagrid,我有一个datagrid,其中的行根据用户的某些偏好显示或消失。 此datagrid是stackpanel的一部分,其中包含一些网格。 问题在于,当用户隐藏某些行时,datagrid高度不会收缩。这仅在windows xp上发生,而在windows 7上不会发生,因为在windows 7中,网格会按预期收缩 下面是一个小示例,说明了问题: xaml: <Window x:Class="TestDataGridResize.MainWindow" xmlns="http://sche

我有一个datagrid,其中的行根据用户的某些偏好显示或消失。 此datagrid是stackpanel的一部分,其中包含一些网格。 问题在于,当用户隐藏某些行时,datagrid高度不会收缩。这仅在windows xp上发生,而在windows 7上不会发生,因为在windows 7中,网格会按预期收缩

下面是一个小示例,说明了问题:

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>

其背后的代码是:

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;
}
公共部分类主窗口:窗口
{
私有ObservableCollection元素=新ObservableCollection();
公共主窗口()
{
初始化组件();
this.dataGrid.ItemsSource=this.elements;
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
对于(int i=0;i<5;i++)
this.elements[i].Visibility=System.Windows.Visibility.Collapsed;
}
已加载私有无效窗口(对象发送器、路由目标)
{
对于(int i=0;i<10;i++)
添加(新元素(“元素”+i));
}
}
公共类元素:INotifyPropertyChanged
{
私有静态属性ChangedEventArgs VisibilityChanged=新属性ChangedEventArgs(“可见性”);
私有字符串名称;
公共字符串名
{
获取{返回this.name;}
设置{this.name=value;}
}
私人能见度;
公众能见度
{
获取{返回this.visibility;}
设置
{
这个。可见性=值;
if(this.PropertyChanged!=null)
this.PropertyChanged(this,VisibilityChanged);
}
}
公共元素(字符串名称)
{
this.name=名称;
}
公共事件属性更改事件处理程序属性更改;
}
目标框架是4.0。
任何帮助都将不胜感激。

尝试使用
网格来排列控件,而不是
堆栈面板

<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>


这解决了问题。非常感谢。
StackPanel
s无法调整其内容元素的大小<代码>网格
的操作。