.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > How to bind Windows.Visibility to view model property
 

How to bind Windows.Visibility to view model property

I've been struggling with this issue for a few hours now where I wonder if there's a limitation or maybe it simply can't be done.
Is it possible to bind the Windows.Visibility property to the view model? My binding path is fine since I have other items bound, plus I've been able to bind the Windows.Background to the view model property.... just not the Visibility property. When I set the binding, nothing happens. It won't even hit the break point in the get property. Here's my code:
In the View XAML:

<Window.Visibility>
<Binding Path="IsVisible" Converter="{StaticResource MyConverter}"/>
</Window.Visibility>

In the view model:
public bool IsVisible {
get { return _isVisible; }
set {
if (this._isVisible != value) {
this._isVisible = value;
OnPropertyChanged("IsVisible");
}
}
}

My break point doesn't hit the Converter class either when I use the Visibility property. I've tested it other bindings in the same View and it works fine. Thanks in advance for any pointers...
sisnaz
Hi sisnaz,

Please try to set Mode property to "TwoWay" like the following:

<Binding Path="IsVisible" Converter="{StaticResource con}" Mode="TwoWay"/>

Let me know if it helps.


Best regards,
Bruce Zhou
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Marked As Answer bysisnaz Wednesday, September 23, 2009 4:40 AM
  •  
Bruce.Zhou
Visibility is not a boolean value. Does your converter accept the boolean value from the model and then converts it to aVisibilityvalue?

Also, are you setting the correct DataContext on the Window?
DeviantSeev
Thanks for the response. Yes, I've tried both the converter for the boolean to Visibility and simply using a Visibility property instead of boolean. The issue right now is .net completely ignores the binding at runtime. No errors or anything. The DataContext is fine, as I said, I can bind the background and controls... just not the Windows.Visibility.
sisnaz
Actually, binding erros are listed in the output window. Are you seeing any?
DeviantSeev
No binding errors in the output window either... I'm stumped.
I set a button with a click event to set this.Visibility = Visibility.Hidden; This works during runtime, but it's the view, not the view model.

sisnaz
That's odd, does the 'get' of the property get fired during runtime?
DeviantSeev
No, does not hit the get property :-(

"It won't even hit the break point in the get property."
sisnaz
No, does not hit the get property :-(

"It won't even hit the break point in the get property."
sisnaz
Hi sisnaz,

Please try to set Mode property to "TwoWay" like the following:

<Binding Path="IsVisible" Converter="{StaticResource con}" Mode="TwoWay"/>

Let me know if it helps.


Best regards,
Bruce Zhou
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Marked As Answer bysisnaz Wednesday, September 23, 2009 4:40 AM
  •  
Bruce.Zhou
Converters are a good way to debug bindings. Since you already have a converter, try putting a breakpoint on the first line of its Convert() function and see if that gets called.
Graeme.Hill
Thanks Bruce! That did the trick... I really appreciate it.
sisnaz
Bruce,

Why shouldn't the 'get' fire in a one way binding?
DeviantSeev

You can use google to search for other answers

Custom Search

More Threads

• Binding and TemplateBinding - the REAL difference.
• TabControl - can't remove right and bottom border?
• Custom Control : Custom TabControl and TabItem
• Problem with InkCanvas Binding
• Save Instance of some Class to ControlTemplate
• What are the advantages & disadvantages of Silverlight?
• PSA: XBAP crash on closing while media is playing
• Forcing .NEt 3.5 SP1
• User control with template like thing
• WPF Video Editor Released