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?
If you already have your bool variable in a viewmodel, you have two things to do:
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:
XAML:
Some Codebehind quick testcode: (actually is MainWindow.xaml.cs)
You can't bind field. You can only bind public properties or dependency properties.
Using public property (you have to implement
INotifyPropertyChanged
interface to have property->binding):The XAML code is:
You don't need to make any converter.
Add a binding to a Visibility property for the border:
And then create the property Visibility in a viewmodel like this:
So, now you can set Visible or Hidden to your Visibility property as follow:
But remember, Visibility enum is located in System.Windows namespace, so your viewmodel has to contain
using System.Windows;
.First you will need to make vis a Property:
Then you will need a ValueConverter.
You will need to create an instance of the converter like so in your resources:
Then you can bind your border like so: