在 WPF 中隐藏网格行 [英] Hide grid row in WPF

查看:77
本文介绍了在 WPF 中隐藏网格行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 WPF 表单,其中声明了一个 Grid.这个 Grid 有一堆行:

I have a simple WPF form with a Grid declared on the form. This Grid has a bunch of rows:

<Grid.RowDefinitions>
    <RowDefinition Height="Auto" MinHeight="30" />
    <RowDefinition Height="Auto" Name="rowToHide" />
    <RowDefinition Height="Auto" MinHeight="30" />
</Grid.RowDefinitions>

名为 rowToHide 的行包含一些输入字段,我想在检测到不需要这些字段后隐藏该行.只需将 Visibility = Hidden 设置为行中的所有项目就足够简单了,但该行仍然占用 Grid 中的空间.我尝试将 Height = 0 设置为项目,但这似乎不起作用.

The row named rowToHide contains a few input fields and I want to hide this row after I detect I don't need these fields. It's simple enough to just set Visibility = Hidden to all items in the row, but the row still takes up space in the Grid. I tried setting Height = 0 to the items, but that didn't seem to work.

您可以这样想:您有一个表单,其中有一个下拉菜单显示付款类型",如果此人选择现金",您希望隐藏包含卡详细信息的行.不能以隐藏的形式启动表单.

You can think of it like this: You have a form, in there you have a drop down saying "Payment Type", and if the person selects "Cash", you want to hide the row containing the Card details. It isn't an option to start the form with this hidden already.

推荐答案

Row 没有Visibility 属性,所以就像别人说的,需要设置Height.另一种选择是使用转换器,以防您在许多视图中都需要此功能:

Row does not have a Visibility property, so as others have said, you need to set the Height. Another option is to use a converter, in case you need this functionality in many views:

    [ValueConversion(typeof(bool), typeof(GridLength))]
    public class BoolToGridRowHeightConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((bool)value == true) ? new GridLength(1, GridUnitType.Star) : new GridLength(0);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {    // Don't need any convert back
            return null;
        }
    }

然后在相应的视图中<Grid.RowDefinition>:

<RowDefinition Height="{Binding IsHiddenRow, Converter={StaticResource BoolToGridRowHeightConverter}}"></RowDefinition>

这篇关于在 WPF 中隐藏网格行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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