Reputation: 1093
I implemented a textbox and it have to become visible when a checkbox is'nt checked and vica versa. Here is my view:
<TextBox Visibility="{Binding VisiMaxTime}" Height="23" HorizontalAlignment="Left" Margin="165,36,0,0" Text="{Binding Path=MaxTime,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" VerticalAlignment="Top" Width="75" />
<CheckBox IsChecked="{Binding MaxTimeIsChecked}" Content="Geen" FontWeight="Bold" Height="16" HorizontalAlignment="Left" Margin="104,39,0,0" Name="checkBox1" VerticalAlignment="Top" />
And this is my viewmodel:
public bool MaxTimeIsChecked
{
get { return maxTimeIsChecked; }
set
{
maxTimeIsChecked = value;
if (maxTimeIsChecked == true)
{
VisiMaxTime = Visibility.Hidden;
this.Examination.MaxTime = 0;
}
else
VisiMaxTime = Visibility.Visible;
OnPropertyChanged("MaxTimeIsChecked");
}
}
private Visibility visiMaxTime;
public Visibility VisiMaxTime
{
get { return visiMaxTime; }
set
{
visiMaxTime = value;
OnPropertyChanged("VisiMaxTime");
}
}
So i think this is good, this works in usercontrols but now i am working in a window. If i set a breakpoint in the setters of Visibility, it works fine, but the textbox just does'nt become visible? Does somebody know what i am doing wrong?
Thanks
Upvotes: 4
Views: 2194
Reputation: 8503
The problem is that the VisiMaxTime property doesn't update the MaxTimeIsChecked property
private Visibility visiMaxTime;
public Visibility VisiMaxTime
{
get { return visiMaxTime; }
set
{
if (visiMaxTime == value)
return;
visiMaxTime = value;
OnPropertyChanged("VisiMaxTime");
MaxTimeIsChecked = VisiMaxTime == Visibility.Visible;
}
}
That being said, you could just bind to the MaxTimeIsChecked property and use a BooleanToVisibilityConverter
Upvotes: 0
Reputation: 3777
if the visiblity of the textbox depends on Checkbox, why not bind to that directly?
<TextBox Visibility="{Binding ElementName=checkBox1,Path=IsChecked,Converter=BooleanToVisibilityConverter}" />
you will need to use BooleanToVisibilityConverter to convert Bool to Visiblity
Upvotes: 4
Reputation: 132558
Is your TextBox's DataContext correct?
I noticed that your MaxTime
property is defined set in your ViewModel as ExaminationTime.MaxTime
, while your TextBox.Text binding is bound to MaxTime
. Is your TextBox's DataContext your ViewModel, or your ExaminationTime?
Upvotes: 0
Reputation: 57919
I imagine you need two-way binding:
<CheckBox IsChecked="{Binding MaxTimeIsChecked, Mode=TwoWay}" … />
Upvotes: 0