开发者

Problem with DataTrigger binding - setters are not being called

I have a Command bound to a Button in XAML. When executed, the command changes a property value on the underlying DataContext. I would like the button's Content to reflect the new value of the property.

This works*:

<Button Command="{x:Static Member=local:MyCommands.TestCommand}"
        Content="{Binding Path=TestProperty, Mode=OneWay}" />

But this doesn't:

<Button Command="{x:Static Member=local:MyCommands.TestCommand}">
  <Button.Style>
    <Style TargetType="{x:Type Button}">
      <Style.Triggers>
        <DataTrigger Binding="{Binding Path=TestProperty, Mode=OneWay}" Value="True">
          <DataTrigger.Setters>
            <Setter Property="Content" Value="Yes"/>
          </DataTrigger.Setters>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=TestProperty, Mode=OneWay}" Value="False">
          <DataTrigger.Setters>
            <Setter Property="Content" Value="No"/>
          </DataTrigger.Setters>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Button.Style>
</Button>

Why is that?

* By "works" I mean the Content gets updated whenever I click the button. I don't know if it's important or not, but just in case it is, I'm using .NET 3.5 SP1.

UPDATE:

I also tried another variation (removing the second DataTrigger in favor of default value), still to no avail:

<Button Comma开发者_如何学JAVAnd="{x:Static Member=local:MyCommands.TestCommand}">
  <Button.Style>
    <Style TargetType="{x:Type Button}">
      <Setter Property="Content" Value="No"/>
      <Style.Triggers>
        <DataTrigger Binding="{Binding Path=TestProperty, Mode=OneWay}" Value="True">
          <DataTrigger.Setters>
            <Setter Property="Content" Value="Yes"/>
          </DataTrigger.Setters>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Button.Style>
</Button>

TIA


Try to set your default Content to No and to use only the first DataTrigger. Avoid the second one.


I solved the problem myself. It turned out I was looking in the wrong place. The bound CLR property was of different type than what I'd assumed in XAML. My stupidity, really.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜