Reputation: 867
So, I'm making a WPF Application using the M-V-VM design pattern and having some trouble getting my Bindings to work properly.
I have a custom ToggleButton, and the way I want it to work is this:
In the xaml, I have this style:
<Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource CustomisableToggleButton}" x:Key="ValidatedTButton">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsChecked}" Value="True" />
<Condition Binding="{Binding IsValid}" Value="True" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Background" Value="Turquoise" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsChecked}" Value="True" />
<Condition Binding="{Binding IsValid}" Value="False" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Background" Value="LightCoral" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<DataTrigger Binding="{Binding IsChecked}" Value="False">
<Setter Property="Background" Value="AliceBlue" />
</DataTrigger>
</Style.Triggers>
</Style>
(CustomisableToggleButton is a catch all style that applies to ToggleButtons - AFAIK this should override the triggers that are inherent in that - correct me if I'm wrong)
In the Control Class:
public class ValidatedToggleButton : ToggleButton
{
public ValidatedToggleButton()
: base() { }
public static readonly DependencyProperty IsValidProperty = DependencyProperty.Register(
"IsValid", typeof(bool), typeof(ValidatedToggleButton));
public bool IsValid
{
get { return (bool)GetValue(IsValidProperty); }
set { SetValue(IsValidProperty, value); }
}
}
And the actual implementation of the control is:
<Window
<!--standard window properties-->
xmlns:cc="clr-namespace:MVVM.CustomControls"> // namespace where 'ValidatedToggleButton' resides
<!--other XAML code-->
<cc:ValidatedToggleButton
IsValid="{Binding Boolean1}"
Content="ToggleButton1"
IsChecked="{Binding ToggleButton1Checked}"
Grid.Row="6" Style="{StaticResource ValidatedTButton}" />
</Window>
Now, the problem is, it never checks the 'Boolean1' value apart from once at startup (verified using breakpoints). How can I make it check that value every time the control is pressed?
Upvotes: 1
Views: 397
Reputation: 132678
In addition to Kent's Answer of fixing your XAML bindings, verify that the PropertyChanged
event is getting raised when Boolean1
is changing.
You can do this by inserting a breakpoint in the get
method.
Upvotes: 1
Reputation: 178820
<Condition Binding="{Binding IsChecked}" Value="True" />
This is looking for a property called IsChecked
on your view model (your data context). Are you sure you don't want this:
<Condition Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}" Value="True" />
Upvotes: 2