c# - why WPF Window.Visibility binding must have Mode=TwoWay for property's get to be called? -


viewmodel:

    public floatingtoolbarwindowviewmodel(guiitems guiitems)     {          guiitemsinstance = guiitems;         guiitemsinstance.host = host;         guiitemsinstance.refreshvisibility = refreshvisibility;     }      private visibility _windowvisibility;     public visibility windowvisibility     {         { return _windowvisibility; }         set         {             //raises propertychanged-event             setvalue(ref _windowvisibility, value);         }     }     // check if of toolbars should in window , sets visibility     public void refreshvisibility(int roleid)     {         if (guiitemsinstance.toolbaritems.any(i => i.toolbarlocation == toolbarlocation.float && i.roleid == roleid))             windowvisibility = visibility.visible;         else             windowvisibility = visibility.hidden;     } 

xaml:

 visibility="{binding windowvisibility, mode=twoway}" 

this means can never work because in end showwindow updates property visible though initialization have "decided" should hidden.

so hack in code behind file:

public partial class floatingtoolbarwindow : window {     public floatingtoolbarwindow()     {         initializecomponent();         contentrendered += floatingtoolbarwindow_contentrendered;     }      private void floatingtoolbarwindow_contentrendered(object sender, eventargs e)     {          ((floatingtoolbarwindowviewmodel)datacontext).refreshwindowvisibility();     } 

viewmodel hack-method:

    public void refreshvisibility()     {         refreshvisibility(guiitemsinstance.activeroleid);     } 

is there way without terrible hack. shouldn't work mode=oneway binding in 1st place?

i'd make windowvisibility readonly property:

public visibility windowvisibility {                  {           if (guiitemsinstance.toolbaritems.any(i => i.toolbarlocation == toolbarlocation.float && i.roleid == roleid))         return visibility.visible;     else         return visibility.hidden;     } } 

bind visibility oneway:

visibility="{binding windowvisibility, mode=oneway}" 

and whenever have "update" visibility raise property changed... don't know using, either

onpropertychanged("windowvisibility"); 

or

onpropertychanged(() => windowvisibility); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -