从 C# 代码动态切换 WPF 网格列的可见性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19227091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:27:47  来源:igfitidea点击:

Dynamically toggle visibility of WPF grid column from C# code

c#wpfxamlgridvisibility

提问by sebi

My problem is: I can't find out how to toggle the visibility of my WPF grid column. Assume following XAML markup:

我的问题是:我不知道如何切换 WPF 网格列的可见性。假设以下 XAML 标记:

<Grid x:Name="myGrid">
    <Grid.RowDefinitions>
        <RowDefinition x:Name="Row1" />
        <RowDefinition x:Name="Row2" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition x:Name="Column1" />
        <ColumnDefinition x:Name="Column2" />
    </Grid.ColumnDefinitions>
</Grid>

Aferwards the grid is filled with some controls etc. Now I want to hide a single column dynamically out of my C# code. I've tried achieving this by setting the the column's definition width to zero, e.g. Column1.Width = 0. This works, but I don't really like this solution - is there really no better way?

Aferwards 网格填充了一些控件等。现在我想从我的 C# 代码中动态隐藏单个列。我尝试通过将列的定义宽度设置为零来实现这一点,例如Column1.Width = 0. 这有效,但我真的不喜欢这个解决方案 - 真的没有更好的方法吗?

I'm looking for something like myGrid.Columns[0].Visibility = COLLAPSEDor Column1.Visibility = HIDDEN. I just can't find something like that - any ideas?

我正在寻找类似myGrid.Columns[0].Visibility = COLLAPSED或的东西Column1.Visibility = HIDDEN。我就是找不到这样的东西 - 有什么想法吗?

回答by Sheridan

The simplest way to do this is to add a named Gridas the top level control in the relevant column that you want to hide. Then you can just hide it and all of its contents as you would any other control:

最简单的方法是Grid在要隐藏的相关列中添加一个名为顶级控件的控件。然后,您可以像隐藏任何其他控件一样隐藏它及其所有内容:

In XAML:

在 XAML 中:

<Grid x:Name="myGrid">
    <Grid.RowDefinitions>
        <RowDefinition x:Name="Row1" />
        <RowDefinition x:Name="Row2" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition x:Name="Column1" />
        <ColumnDefinition x:Name="Column2" />
    </Grid.ColumnDefinitions>
    <Grid x:Name="GridColumn1" Grid.Column="1">
        ...
    </Grid>
</Grid>

Then in code behind:

然后在后面的代码中:

GridColumn1.Visibility = Visibility.Collapsed;

As you have more than one row in your Grid, you may want to rearrange them like this:

由于您的 中有不止一行Grid,您可能希望像这样重新排列它们:

<Grid x:Name="myGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition x:Name="Column1" />
        <ColumnDefinition x:Name="Column2" />
    </Grid.ColumnDefinitions>
    <Grid x:Name="GridColumn0" Grid.Column="0">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
    </Grid>
    <Grid x:Name="GridColumn1" Grid.Column="1">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
    </Grid>
</Grid>

UPDATE >>>

更新 >>>

It is not really necessary to rearrange the main Gridlike this... you couldjust as easily add two Gridcontrols, one in each row of the relevant column and then set the Visibilityof them both together:

真的没有必要Grid像这样重新排列主...你可以很容易地添加两个Grid控件,在相关列的每一行一个,然后将Visibility它们设置在一起:

InnerGrid1.Visibility = InnerGrid2.Visibility = Visibility.Collapsed;

You could even add a Gridinto each cell of the main Gridand have full control over which cells are visible at any one time.

您甚至可以Grid在主的每个单元格中添加一个,Grid并在任何时候完全控制哪些单元格可见。

回答by sexta13

In WPF Grid Column and Row Hidingon Code Project, they show a way using dependency properties. They set Visible = false, but internally, it sets Width = 0.

WPF Grid Column 和 Row Hidingon Code Project 中,他们展示了一种使用依赖属性的方法。他们设置了Visible = false,但在内部,它设置了Width = 0

Another idea is to delete the column definition in code behind... But I guess it's an even worse hack! :(

另一个想法是删除后面代码中的列定义......但我想这是一个更糟糕的黑客!:(

回答by Dave

An ugly hack would be to just change the width to 0 (out of sight, out of mind).

一个丑陋的黑客将只是将宽度更改为 0(看不见,心不在焉)。

There are many reasons why you shouldn't do this but, based upon your situation, it may suffice!

不应该这样做的原因有很多,但根据您的情况,这可能就足够了!

回答by Raj

Use following to Hide the grid. Similarly you can hide the row definitions and column definitions using Name property.

使用以下隐藏网格。同样,您可以使用 Name 属性隐藏行定义和列定义。

myGrid.Visibility = System.Windows.Visibility.Hidden;

myGrid.Visibility = System.Windows.Visibility.Hidden;

回答by Morlo Mbakop

<ColumnDefinition>
  <ColumnDefinition.Style>
    <Style TargetType="ColumnDefinition">
      <Setter Property="Width" Value="*" />
        <Style.Triggers>
          <DataTrigger Binding="{Binding IsColumnVisible}" Value="False">
            <Setter Property="Width" Value="0" />
          </DataTrigger>
        </Style.Triggers>
    </Style>
  </ColumnDefinition.Style>
</ColumnDefinition>

Please do implement INotifyPropertyChanged in your ViewModel

请在您的 ViewModel 中实现 INotifyPropertyChanged