无法在XAML中更改Button.Content

时间:2012-06-12 17:08:00

标签: wpf triggers

我想使用DataTrigger更改XAML中Button的内容。有条件地我需要更改按钮的文本。但是,如果Button最初有文本,则不会更改文本。使其工作的唯一方法是不设置Content,或者使用触发器设置它。

所以,如果我有

<Button Content="Some text" />

触发器不会更改按钮文本。

如果我有

<Button />

<Button>
    <Button.Style>
        <Style TargetType="Button">
            <Setter Property="Content" Value="Some text" />
        </Style>
    </Button.Style>
</Button>

触发器有效。

为什么?

1 个答案:

答案 0 :(得分:1)

这是因为DepedencyProperty优先级。见这里:http://msdn.microsoft.com/en-us/library/ms743230.aspx

本地值的优先顺序高于触发器。这意味着本地值将“优先于”触发值

在样式中设置时,它会更改优先级。触发器优先于样式设置器,这就是它按预期工作的原因。