Button和datatrigger不起作用

时间:2012-12-17 22:51:28

标签: c# wpf xaml

我想用这个触发器创建一个按钮。但是这不起作用.Even custom datatemplte没有。这失败了

  

{“触发器集合成员必须是EventTrigger类型。”}

<Button cal:Message.Attach="Switch"  Width="55" HorizontalAlignment="Left" 
        Style="{StaticResource OrderProductButton}"  Margin="6,4,0,7">

    <StackPanel Orientation="Horizontal">
        <TextBlock x:Name="literal" Foreground="White" 
                   xml:space="preserve">€</TextBlock>
        <TextBlock Foreground="White"  xml:space="preserve"> / </TextBlock>
        <TextBlock x:Name="percent" Foreground="Gray"> %</TextBlock>
    </StackPanel>

    <Button.Triggers>
        <DataTrigger Binding="{Binding IsPercent}" Value="True">
            <Setter Property="TextBlock.FontWeight" Value="Bold" 
                    TargetName="percent"  />
            <Setter Property="TextBlock.Foreground" Value="White" 
                    TargetName="percent"  />
        </DataTrigger>
        <DataTrigger Binding="{Binding IsPercent}" Value="false">
            <Setter Property="TextBlock.FontWeight" Value="Bold" 
                    TargetName="literal"  />
            <Setter Property="TextBlock.Foreground" Value="White" 
                    TargetName="literal"  />
        </DataTrigger>
    </Button.Triggers>
</Button>

1 个答案:

答案 0 :(得分:1)

使用样式是否适用于您?

<Style TargetType="Button"> <!-- use a key if you want to target only specific buttons -->
  <Style.Triggers>
    <DataTrigger Binding="{Binding IsPercent}" Value="True">
        <Setter Property="TextBlock.FontWeight" Value="Bold" 
                TargetName="percent"  />
        <Setter Property="TextBlock.Foreground" Value="White" 
                TargetName="percent"  />
    </DataTrigger>
    <DataTrigger Binding="{Binding IsPercent}" Value="false">
        <Setter Property="TextBlock.FontWeight" Value="Bold" 
                TargetName="literal"  />
        <Setter Property="TextBlock.Foreground" Value="White" 
                TargetName="literal"  />
    </DataTrigger>
  </Style.Triggers>
</Style>