Kristian Fenn
Kristian Fenn

Reputation: 867

Controlling a style based on a DependencyProperty

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:

  1. When the IsChecked property of the control is set to true, then it should query a second boolean (contained within the ViewModel) is also true.
  2. If it is, set the background colour to one colour, if not then set it to a different colour.
  3. If IsChecked is false, use the standard colour.

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

Answers (2)

Rachel
Rachel

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

Kent Boogaart
Kent Boogaart

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

Related Questions