Am I using DependencyProperties incorrectly?

I defined 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 that fires a style based on the value of my dependency property. In this case, bold if CanSavetrue

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

I get an error

"Binding" cannot be set to the "Property" property of the "Trigger" type. Binding can only be set to DependencyProperty DependencyObject.

I’m probably doing something wrong. Can anyone fix 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 found that CanSave was never set to true, then I did

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

since 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 , . , WPF ?

+3
3

Trigger , :

<Trigger Property="CanSave" Value="True">
+2

, , Value = "True" Value = "true".

, , , . , TextBlock, targettype - TextBlock, Text "True" "False", TextBlock Text dependency (CanSave/Content)

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

,

<TextBlock Style="{StaticResource CanSaveIndicatorHeader}" Text="{Binding CanSave}"/>
0

I found that in some (possibly all) cases when the trigger is used inside the style, you also need to specify the opposite setter outside the trigger: for example:

<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>
0
source

Source: https://habr.com/ru/post/1769941/


All Articles