开发者

Am I using DependencyProperties wrongly?

开发者 https://www.devze.com 2023-01-20 02:33 出处:网络
I have declared my dependency properties as follows public bool CanSave { get { return (bool)GetValue(CanSaveProperty); }

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?


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

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


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}"/>


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号