mvvm - Set the Visibility of Data Grid in WPF -


in application have 3 data grids in single xaml file. based on user selection want show 1 grid , hide other grids.

in view model class have boolean property each grid , based on selection setting true or false.but grids visible .

    <datagrid  visibility="{binding path=isgridvisible}" > 

in view model setting isgridvisible value

public bool iscapexgridvisible     {         { return iscapexgridvisible; }         set { iscapexgridvisible = value; raisepropertychangedevent("iscapexgridvisible"); }     } 

please provide ideas. thanks

visibility property on uielement not boolean. enum 3 values:

collapsed not display element, , not reserve space in layout.

hidden not display element, reserve space element in layout.

visible display element.

so in order set viewmodel should: - make property type of visibility (not best solution in world) - use converter binding trick of translating boolean visibility

  public class booleantocollapsedconverter : ivalueconverter {     public object convert(object value, type targettype, object parameter, cultureinfo culture)     {         if (targettype == typeof(visibility) && value 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();     } } 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -