Search code examples
c#wpfxamlbordervisibility

Bind visibility property to a variable


I have a Border with Label inside a Window,

<Border x:Name="Border1" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="21" Margin="229,164,0,0" VerticalAlignment="Top" Width="90" Opacity="0.5">
    <Grid>
        <Label Content="test"/>
    </Grid>
</Border>

I have also a Variable:

public bool vis = false;

How could I bind the vis variable with border Visibility property?


Solution

  • If you already have your bool variable in a viewmodel, you have two things to do:

    1. make it a property, like:

      public bool vis { get; set; }

    And you need a visibility converter for your property then:

    It is described here:

    http://social.msdn.microsoft.com/Forums/en/wpf/thread/3c0bef93-9daf-462f-b5da-b830cdee23d9

    The example assumes that you have a viewmodel and use Binding

    Here is some demo code that I made from your snippet:

    ViewModel:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    
    namespace StackOverflowWpf2
    {
        public class BorderViewModel : INotifyPropertyChanged
        {
            private bool borderVisible = false;
    
            public bool BorderVisible 
            {
                get
                {
                    return borderVisible;
                }
    
                set
                {
                    borderVisible = value;
                    NotifyPropertyChanged("BorderVisible");
                }
            }
    
            private void NotifyPropertyChanged(string info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
        }
    }
    

    XAML:

    <Window x:Class="StackOverflowWpf2.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">
        <Window.Resources>
            <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
        </Window.Resources>
        <Grid>
            <Border x:Name="Border1" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="21" Margin="229,164,0,0" VerticalAlignment="Top" Width="90" Opacity="0.5"
                    Visibility="{Binding Path=BorderVisible, Converter={StaticResource BoolToVisConverter} }" >
                <Grid>
                    <Label Content="test"/>
                </Grid>
            </Border>
            <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="381,35,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" 
                    />
        </Grid>
    </Window>
    

    Some Codebehind quick testcode: (actually is MainWindow.xaml.cs)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace StackOverflowWpf2
    {
        /// <summary>
        /// Interaktionslogik für MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public BorderViewModel ViewModel { get; set; }
    
            public MainWindow()
            {
                InitializeComponent();
    
                ViewModel = new BorderViewModel();
    
                this.DataContext = ViewModel;
    
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                var vis = (this.DataContext as BorderViewModel).BorderVisible;
    
                (this.DataContext as BorderViewModel).BorderVisible = !vis;
    
            }
        }
    }