简体   繁体   中英

How to bind data to a control's Visibility property

I understand that the Visibility property of a control cannot be bound to data in the same way that other properties can. It needs some kind of converter(?). In trying to implement the solution from this question I run into a compiler error that says: The resource "BoolToVisible" could not be resolved . I'm guessing that I have to create a ResourceKey named BoolToVisible , I just don't know how.

I'm requesting that someone show me the right way to Bind to the Visibility property of a control.

*The control that I am adding this to is a radio button. * I have a bool property for isVisible in my Data Model that will be bound to this radio button.

Data Model Property:

private bool _isVisible = true;

public bool IsVisible
{
       get { return _isVisible; }
       set
       {
           _isVisible = value;
           NotifyPropertyChange(() => IsVisible);
       }
}

XAML:

<RadioButton Visibility="{Binding DataModel.IsVisible,Converter={StaticResource ResourceKey=BoolToVisible},RelativeSource={RelativeSource TemplatedParent}}" ... />

Thank you.

2 examples :

The first using a Converter like stated in the question :

  public class BooleanToVisibilityConverter : IValueConverter 
  {
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
     {            
         if (value == null || !(value is bool)) 
             return Binding.DoNothing;

         return (bool)value ? Visibility.Visible : Visibility.Collapsed; 
     }

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
     {
         return value;
     }
 }

in xaml :

<Window x:Class="Stackoverflow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    xmlns:local="clr-namespace:Stackoverflow"
    >
<Window.Resources>
    <local:BooleanToVisibilityConverter x:Key="booleanToVisibiltyConverter"/>
</Window.Resources>

<Grid>
    <Button Visibility="{Binding IsSomeThing,Converter={StaticResource booleanToVisibiltyConverter}}"/>        
</Grid>

the second :

in your DataContext you can literly hold a Visibility Property

cs :

    private Visibility _myControlVisibility;
    public Visibility MyControlVisibility
    {
        get { return _myControlVisibility; }
        set { _myControlVisibility = value; }
    }

xaml :

    <Button Visibility="{Binding MyControlVisibility}"/>        

You can binding visibility with a property, you just need to have a Dependency Property of Visibility field as:

    Public Property MyVisibility As Windows.Visibility
    Get
        Return GetValue(MyVisibilityProperty)
    End Get

    Set(ByVal value As Windows.Visibility)
        SetValue(MyVisibilityProperty, value)
    End Set
End Property

Public Shared ReadOnly MyVisibilityProperty As DependencyProperty = _
                       DependencyProperty.Register("MyVisibility", _
                       GetType(Windows.Visibility), GetType(MyWindow), _
                       New PropertyMetadata(Nothing))

Then do the binding as the usual (The code is in VB).

Remember that in the New PropertyMetadata you can set an initial state for the object eg:

 Public Shared ReadOnly MyVisibilityProperty As DependencyProperty = _
                       DependencyProperty.Register("MyVisibility", _
                       GetType(Windows.Visibility), GetType(MyWindow), _
                       New PropertyMetadata(Windows.Visibility.Hidden))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM