udaya726
udaya726

Reputation: 1020

Set the Visibility of Data Grid in WPF

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.

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 .

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

In my view model I am setting IsGridVisible value

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

Please provide your ideas. Thanks

Upvotes: 1

Views: 11541

Answers (3)

Genusatplay
Genusatplay

Reputation: 771

Additional converter variant with visibility customization

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;


[MarkupExtensionReturnType(typeof(IValueConverter))]
public class BoolToVisibilityConverter : MarkupExtension, IValueConverter
{
    [ConstructorArgument("TrueValue")]
    public Visibility TrueValue { get; set; }

    [ConstructorArgument("FalseValue")]
    public Visibility FalseValue { get; set; }

    [ConstructorArgument("NullValue")]
    public Visibility NullValue { get; set; }

    public BoolToVisibilityConverter()
    {
        TrueValue = Visibility.Visible;
        FalseValue = Visibility.Collapsed;
        NullValue = Visibility.Collapsed;
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null) return NullValue;

        if (value is not bool boolValue)
            return null;

        return boolValue ? TrueValue : FalseValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (Equals(value, TrueValue))
            return true;
        if (Equals(value, FalseValue))
            return false;
        return null;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

Usage:

<someControl ...
             xmlns:converters="clr-namespace:ExampleNamespace.Converters;assembly=ExampleAssembly"
             ...
             >

...

Visibility="{Binding IsSearchInProgress, 
             Mode=OneWay, 
             Converter={converters:BoolToVisibilityConverter}}"

Visibility="{Binding IsSearchInProgress, 
             Mode=OneWay, 
             Converter={converters:BoolToVisibilityConverter TrueValue=Collapsed, FalseValue=Visible}}"

Upvotes: 0

Nitesh
Nitesh

Reputation: 7409

There is a BooleanToVisibilityConverter available to you that converts true to System.Windows.Visibility.Visible and false to System.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 bool in your 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}}"/>

Upvotes: 10

Sebastian &#208;ymel
Sebastian &#208;ymel

Reputation: 717

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

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

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

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

  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();
    }
}

Upvotes: 2

Related Questions