c# - Hide a column in the grid which is a layout element in WPF using BooleanToVisibility converter -


i trying bind visibility property of grid column using boolean visibility converter. grid visible though bool value false. please me fix issue. code works fine datagrid not grid .here code.

 <window x:class="myapp.firstwindow"      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:viewmodel="clr-namespace:myapp.viewmodel"      xmlns:i="clr-namespace:system.windows.interactivity;  assembly=system.windows.interactivity"     xmlns:y="clr-namespace:myapp.framework.converter"     xmlns:cmd="clr-namespace:galasoft.mvvmlight.command;assembly=galasoft.mvvmlight.extras.wpf4"      datacontext="{binding myappviewmodel, source={staticresource locator}}"     minheight="530" minwidth="275" height="530" width="275" title="{binding title}"  >   <window.resources>          <y:booltovisibilityconverter x:key="booltovisibilityconverter" /> </window.resources>   <grid margin="5">     <grid.rowdefinitions>         <rowdefinition height="auto"/>         <rowdefinition/>     </grid.rowdefinitions>     <grid.columndefinitions>         <columndefinition/>     </grid.columndefinitions>          <grid grid.row="1" grid.column="1" name="gridtest"        datacontext="{binding griddatacontext, mode=twoway}"           visibility="{binding gridvisibility, mode= twoway, converter=   {staticresource booltovisibilityconverter}}" >         <grid.rowdefinitions>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition height="auto"/>             <rowdefinition/>         </grid.rowdefinitions>         <grid.columndefinitions>             <columndefinition width="auto"/>             <columndefinition/>         </grid.columndefinitions>          <label >date</label>         <label grid.column="1" content="{binding date}" />          <label grid.row="1">time</label>         <label grid.row="1" grid.column="1" content="{binding time}"/>         </grid> </grid> 

booltovisibilityconverter.cs

  using system;   using system.collections.generic;   using system.linq;   using system.text;   using system.windows;   using system.windows.data;   using system.globalization;    namespace myapp.framework.converter   {    public sealed class booltovisibilityconverter : ivalueconverter     {      public object convert(object value, type targettype, object     parameter,         system.globalization.cultureinfo culture)     {         if (parameter == null)         {             return ((bool)value == true) ? visibility.visible : visibility.hidden;         }         else if (parameter.tostring() == "inverse")         {             return ((bool)value == true) ? visibility.collapsed : visibility.visible;         }         return false;     }      public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         throw new notimplementedexception();     } 

myappviewmodel.cs

public class myappviewmodel : viewmodelbase, inotifypropertychanged {       private bool _isgridvisible = false;       private bool _islabelvisible = false;      private myitem _griddatacontext;      public myitem griddatacontext     {         { return _griddatacontext; }         set         {             _griddatacontext = value;             onpropertychanged("griddatacontext");         }     }       public bool islabelvisible     {                 {             return _islabelvisible;         }          set         {             if (_islabelvisible == value)             {                 return;             }              _islabelvisible = value;             onpropertychanged("islabelvisible");         }     }     public bool gridvisibility     {                 {             return _isgridvisible;         }          set         {             if (_isgridvisible == value)             {                 return;             }              _isgridvisible = value;             onpropertychanged("gridvisibility");         }     }       /// <summary>-     /// initializes new instance of appviewmodel class.     /// </summary>           public appviewmodel()     {         _griddatacontext = new myitem();         //other code.     }   private void updatemyitem()     {         try         {             bool wasset = false;              if (test== null)             {                 setvisible(false, "no feature selected.");             }             else             {                 setvisible(false, string.format("no test                          information  selected item {0}.",                      test.name.tostring().padleft(5, '0')));             }          }    private void setvisible(bool wasset, string msg)     {          //under construction         if (wasset )         {             dispatcher.currentdispatcher.dynamicinvoke(delegate()             {                 gridvisibility = true;                 islabelvisible = false;              });         }         else if (!wasset )                     {             dispatcher.currentdispatcher.dynamicinvoke(delegate()             {                 gridvisibility = false;                 islabelvisible = true;                 labelcontent = msg;              });         }     }  } 

}

i haven't found answers appropriate doing. blogs have answers hiding/showing datagrid columns , not grid layout element. possible? xaml visibility property of grid ok? help.

solution: myappviewmodel.cs

      private void setvisible(bool wasset, string msg)       {            if (wasset)          {              dispatcher.currentdispatcher.dynamicinvoke(delegate()              {                  griddatacontext.gridvisibility = true;                 islabelvisible = false;                                 });          }          else if (!wasset)                     {              dispatcher.currentdispatcher.dynamicinvoke(delegate()              {                  griddatacontext.gridvisibility = false;                              islabelvisible = true;                  labelcontent = msg;                                 });          }      } 

as stated @ ayyappan subramanian it's due different datacontexts.

window's context myappviewmodel has gridvisibility property implemented grid's context value of griddatacontext property within myappviewmodel (typeof myitem).

so need point right datacontext when binding visibility

<grid grid.row="1"        grid.column="1"       name="gridtest"       datacontext="{binding griddatacontext}"       visibility="{binding datacontext.gridvisibility,                     mode=twoway,                     relativesource={relativesource findancestor, ancestortype=window},                    converter={staticresource booltovisibilityconverter}}"> 

or if not option, change implementation of myitem include given property.

public class myitem : viewmodelbase {     // current implementation goes here...      // gridvisibility implementation     private bool _isgridvisible = false;     public bool gridvisibility     {         { return _isgridvisible; }         set         {             _isgridvisible = value;             raisepropertychanged();         }     } } 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -