Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding Visibility in XAML to a Visibility property

I've seen on the internet quite a few examples of binding a boolean to the Visibility property of a control in XAML. Most of the good examples use a BooleanToVisibiliy converter.

I'd like to just set the Visible property on the control to bind to a System.Windows.Visibility property in the code-behind, but it doesn't seem to want to work.

This is my XAML:

<Grid x:Name="actions" Visibility="{Binding Path=ActionsVisible, UpdateSourceTrigger=PropertyChanged}" />

This is the code for the property:

private Visibility _actionsVisible;
public Visibility ActionsVisible
{
   get
   {
      return _actionsVisible;
   }
   set
   {
      _actionsVisible = value;
   }
}

In the constructor of the Window, I also have this call:

base.DataContext = this;

When I update either ActionsVisible or this.actions.Visibility, the state doesn't transfer. Any ideas to what might be going wrong?

like image 663
Jared Avatar asked Dec 21 '08 18:12

Jared


People also ask

How does binding work in XAML?

When data binding is declared on XAML elements, they resolve data binding by looking at their immediate DataContext property. The data context is typically the binding source object for the binding source value path evaluation.

What is WPF visibility?

In WPF UIElement. Visibility has 3 states; Visible/Hidden/Collapsed. If hidden, the control will still affect the layout of surrounding controls, elements that have a Visibility value of Collapsed do not occupy any layout space. You can switch between visible and hidden or collapsed.

What is binding mode in WPF?

Default Data-bindingIt just defines which is the default binding mode for the control's property. In WPF different controls has different default data-binding modes. For example, TextBlock's Text property has one-way as default binding mode but a TextBox's Text property has a two-way binding mode.

What are the types of binding in WPF?

Datacontext binding Property Binding with change notification from source Property Binding with change notification from client FallBack value Element Binding Binding with Converter Datacontext binding If any other source is not specified for data bindings then WPF by default searches the data Context.


2 Answers

Change your property to be a DependencyProperty. This will handle the updating for you.

        public Visibility ActionsVisible
    {
        get { return (Visibility)GetValue(ActionsVisibleProperty); }
        set { SetValue(ActionsVisibleProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ActionsVisible.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ActionsVisibleProperty =
        DependencyProperty.Register("ActionsVisible", typeof(Visibility), typeof(FooForm));
like image 89
NR. Avatar answered Sep 20 '22 19:09

NR.


I think the problem is that WPF can't know that your ActionsVisible property has changed since you've not notified the fact.

Your class will need to implement INotifyPropertyChanged, then in your set method for ActionsVisible you'll need to fire the PropertyChanged event with ActionsVisible as the property name that has changed.

Hope this helps...

like image 38
Craig Shearer Avatar answered Sep 22 '22 19:09

Craig Shearer