views:

62

answers:

3

I have declared my dependency properties as follows

public bool CanSave
{
    get { return (bool)GetValue(CanSaveProperty); }
    set { SetValue(CanSaveProperty, value); }
}

public static readonly DependencyProperty CanSaveProperty =
    DependencyProperty.Register("CanSave", typeof(bool), typeof(EditorTabViewModel), new PropertyMetadata(false));

In XAML, I want to have a trigger thats triggers the style based on the value of my dependency property. In this case bold if CanSave is true

<Style x:Key="CanSaveIndicatorHeader">
    <Style.Triggers>
        <Trigger Property="{Binding CanSave}" Value="True">
            <Setter Property="TextBlock.FontWeight" Value="Bold" />
        </Trigger>
    </Style.Triggers>
</Style>

i am getting the error

A 'Binding' cannot be set on the 'Property' property of type 'Trigger'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

I am probably doing something wrong. Can someone correct me?

UPDATE: In response to @Bryan Watts

Ok, so I did something like

<Style.Triggers>
    <Trigger Property="vm:EditorTabViewModel.CanSave" Value="true">
        <Setter Property="TextBlock.FontWeight" Value="Bold" />
    </Trigger>
    <Trigger Property="vm:EditorTabViewModel.CanSave" Value="false">
        <Setter Property="TextBlock.Foreground" Value="Red" />
    </Trigger>
</Style.Triggers>

Then i discovered that CanSave is never set to true, then I did

<TextBox ... Text="{Binding Path=Content, UpdateSourceTrigger=PropertyChanged}" />

as CanSave is set to true when Content changes

public string Content
{
    ...
    set
    {
        if ((bool)GetValue(CanSaveProperty) == false)
        {
            SetValue(CanSaveProperty, true);
            RaisePropertyChanged("CanSave");
        } 
        _content = value;
    }
}

But it seems

<Trigger Property="vm:EditorTabViewModel.CanSave" Value="true">

nv happens as the font is never bold. It seems WPF doesn't detect the change?

+2  A: 

The Trigger attribute just needs the name of the property, not a binding to it:

<Trigger Property="CanSave" Value="True">
Bryan Watts
Thanks. but see update: In response to @Bryan Watts. It seems WPF doesn't detect the change?
jiewmeng
@jiewmeng: Can you edit your question to include the declaration of the `CanSave` property? The syntax you are using, with the name namespace and class name, implies it is an attached property. Have you tried your recent updates using `CanSave` instead of `vm:EditorTabViewModel.CanSave`?
Bryan Watts
The declaration of `CanSave` if in the 1st code snippet? or did you ask for something else? If I just did `CanSave`, I get something like "Cannot resolve the Style Property 'CanSave'. Verify that the owning type is the Style's TargetType, or use Class.Property syntax to specify the Property"
jiewmeng
Yes, sorry, I missed the Register call. I am not certain why the trigger isn't working for you then.
Bryan Watts
A: 

In the trigger that you defined later value should be Value="True" instead of Value="true".

The trigger could be made simpler if you define the target type of your style and then use the property which you are going to bind. Like in your case you want to apply the style in TextBlock, targettype is TextBlock, add a trigger for Text property with Value "True" and "False" and then bind TextBlock's Text property with dependency property (CanSave / Content)

Style,

    <Style x:Key="CanSaveIndicatorHeader" TargetType="TextBlock">
        <Style.Triggers>
            <Trigger Property="Text" Value="True">
                <Setter Property="TextBlock.FontWeight" Value="Bold" />
            </Trigger>
        </Style.Triggers>
    </Style>

Binding,

<TextBlock Style="{StaticResource CanSaveIndicatorHeader}" Text="{Binding CanSave}"/>
bjoshi
A: 

I have found that in some (possibly all) cases when a trigger is used inside a style you also need to specify the opposite setter outside of the trigger: e.g:

<Style x:Key="CanSaveIndicatorHeader">
    <Setter Property="TextBlock.FontWeight" Value="Normal" />
    <Style.Triggers>
        <Trigger Property="{Binding CanSave}" Value="True">
            <Setter Property="TextBlock.FontWeight" Value="Bold" />
        </Trigger>
    </Style.Triggers>
</Style>
Andy Lowry