我是否错误地使用了DependencyProperties?

3
我已经声明了我的依赖属性如下:
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));

在XAML中,我想要一个触发器,基于我的依赖属性的值来触发样式。在这种情况下,如果CanSave为true,则加粗。
<Style x:Key="CanSaveIndicatorHeader">
    <Style.Triggers>
        <Trigger Property="{Binding CanSave}" Value="True">
            <Setter Property="TextBlock.FontWeight" Value="Bold" />
        </Trigger>
    </Style.Triggers>
</Style>

我遇到了错误:

无法在“触发器”的类型为“属性”的“Property”属性上设置“绑定”。只能在DependencyObject的DependencyProperty上设置“绑定”。

我可能做错了什么。有人可以指正一下吗?

更新:回应@Bryan Watts

好的,我做了类似于以下的事情:

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

然后我发现CanSave从未设置为true,于是我进行了操作。
<TextBox ... Text="{Binding Path=Content, UpdateSourceTrigger=PropertyChanged}" />

当内容更改时,由于CanSave设置为true

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

但是看起来
<Trigger Property="vm:EditorTabViewModel.CanSave" Value="true">

字体未加粗,可能是因为 WPF 没有检测到修改?

3个回答

2
< p > Trigger 属性只需属性名称,而不是绑定到它:< /p >
<Trigger Property="CanSave" Value="True">

谢谢。但请看更新:回应 @Bryan Watts。似乎 WPF 没有检测到变化? - Jiew Meng
@jiewmeng:你能编辑一下你的问题,包括CanSave属性的声明吗?你使用的语法,包含命名空间和类名,暗示它是一个附加属性。你尝试过使用CanSave而不是vm:EditorTabViewModel.CanSave进行最近的更新了吗? - Bryan Watts
第一段代码片段中有CanSave的声明吗?还是你要求别的什么内容?如果我只写了 CanSave,会出现以下提示信息:"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"。 - Jiew Meng
是的,抱歉,我错过了注册调用。我不确定为什么触发器对你不起作用。 - Bryan Watts

0
在您后面定义的触发器中,值应该是 Value="True" 而不是 Value="true"。
如果定义样式的目标类型并使用要绑定的属性,则可以使触发器更简单。就像您的情况一样,您想在 TextBlock 中应用样式,目标类型是 TextBlock,在 Text 属性上添加一个值为 "True" 和 "False" 的触发器,然后将 TextBlock 的 Text 属性与依赖属性 (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
我发现在某些情况下(可能是所有情况),当在样式内使用触发器时,您还需要在触发器外指定相反的设置器:例如:
<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接