在 WPF 中设置数据网格的可见性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18139890/
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-09-13 09:24:05  来源:igfitidea点击:

Set the Visibility of Data Grid in WPF

wpfmvvmwpfdatagrid

提问by udaya726

In my application I have 3 data grids in a single xaml file. Based on the User selection I want show one grid and hide other grids.

在我的应用程序中,我在一个 xaml 文件中有 3 个数据网格。根据用户选择,我想显示一个网格并隐藏其他网格。

in my view model class I have Boolean property for each grid and based on the selection I am setting it to true or false.But all grids are visible .

在我的视图模型类中,每个网格都有布尔属性,并根据选择将其设置为 true 或 false。但所有网格都是可见的。

    <DataGrid  Visibility="{Binding Path=IsGridVisible}" >

In my view model I am setting IsGridVisible value

在我的视图模型中,我正在设置 IsGridVisible 值

public bool IsCapexGridVisible
    {
        get { return isCapexGridVisible; }
        set { isCapexGridVisible = value; RaisePropertyChangedEvent("IsCapexGridVisible"); }
    }

Please provide your ideas. Thanks

请提供您的想法。谢谢

回答by Nitesh

There is a BooleanToVisibilityConverteravailable to you that converts trueto System.Windows.Visibility.Visibleand falseto System.Windows.Visibility.Collapsed.

有一个BooleanToVisibilityConverter提供给您的是转换trueSystem.Windows.Visibility.VisiblefalseSystem.Windows.Visibility.Collapsed

So you can take help of this pre built converter and must add it to resources.

因此,您可以借助这个预先构建的转换器,并且必须将其添加到资源中。

<BooleanToVisibilityConverter x:Key="BoolToVis"/>

Create a property of type boolin your ViewModel

bool在您的 ViewModel 中创建类型的属性

    bool _dgVisibility;
    public bool DataGridVisibility
    {
        get {  return _dgVisibility;  }
        set
        {
            _dgVisibility = value;
            OnPropertyChanged("DataGridVisibility");
        }
    }

and you can use it as below

你可以使用它如下

<DataGrid Visibility="{Binding Path=DataGridVisibility, Converter={StaticResource BoolToVis}}"/>

回答by Sebastian Dymel

Visibility property on UIElement is not a boolean. It is an enum with three values:

UIElement 上的可见性属性不是布尔值。它是一个具有三个值的枚举:

CollapsedDo not display the element, and do not reserve space for it in layout.

Collapsed不显示元素,也不在布局中为其预留空间。

HiddenDo not display the element, but reserve space for the element in layout.

Hidden不显示元素,但在布局中为元素保留空间。

VisibleDisplay the element.

可见显示元素。

So in order to set it properly from ViewModel you should: - make your property type of Visibility (not best solution in the world) - Use converter for the binding which will do the trick of translating boolean to visibility

因此,为了从 ViewModel 正确设置它,您应该: - 使您的属性类型为 Visibility (不是世界上最好的解决方案) - 使用转换器进行绑定,这将完成将布尔值转换为可见性的技巧

  public class BooleanToCollapsedConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (targetType == typeof(Visibility) && value is bool)
        {
            return (bool)value ? Visibility.Visible : Visibility.Collapsed;
        }
        throw new FormatException();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}