动态切换从C#代码WPF网格列的知名度 [英] Dynamically toggle visibility of WPF grid column from C# code

查看:140
本文介绍了动态切换从C#代码WPF网格列的知名度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

<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网格充满了一些控制等。现在我要动态地隐藏单列出来我的C#代码。我试着在列的定义宽度设置为零实现这一目标,例如 Col​​umn1.Width = 0 。这工作,但我真的不喜欢这个解决方案 - 是真的没有更好的办法。

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?

我正在寻找类似 myGrid?专栏[0] = .Visibility COLLAPSED Col​​umn1.Visibility = HIDDEN 。我只是找不到类似的东西 - 任何想法

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

推荐答案

要做到这一点最简单的方法是添加一个名为电网如要隐藏相应列的最高级别控制。然后,你可以将其隐藏和它的所有内容,就像任何其他的控制:

The simplest way to do this is to add a named Grid as 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:

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

然后在后面的代码:

GridColumn1.Visibility = Visibility.Collapsed;



当你有多个行的电网,你可能要重新排列它们是这样的:

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

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

更新>>>

有不是真的有必要重新整理主电网这样的......你的可能的很容易地添加两个电网控件,一个相关列的每一行中,然后设置能见度在一起:

It is not really necessary to rearrange the main Grid like this... you could just as easily add two Grid controls, one in each row of the relevant column and then set the Visibility of them both together:

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

您甚至可以添加一个电网到每个主电网并具有完全控制哪些细胞是在任何一个时间可见的细胞。

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

这篇关于动态切换从C#代码WPF网格列的知名度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆