ToggleButton / CheckBox内容取决于其检查状态?

时间:2012-04-03 03:30:41

标签: silverlight checkbox triggers silverlight-5.0 togglebutton

制作ToggleButton内容的最短xamly方式取决于其检查状态是什么?

在WPF中,我可能会选择Silverlight中不存在的DataTrigger

我尝试了以下操作,但它不起作用,只要我包含触发器,就会破坏对源的绑定。触发器无论如何都不会起作用。

<ToggleButton
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
IsChecked="{Binding IsArchived, Mode=TwoWay}">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Checked">
      <ei:ChangePropertyAction
      TargetObject="{Binding
        RelativeSource={RelativeSource AncestorType=ToggleButton}}" 
      PropertyName="Content" Value="Unarchive project"/>
    </i:EventTrigger>
    <i:EventTrigger EventName="Unchecked">
      <ei:ChangePropertyAction 
      TargetObject="{Binding 
        RelativeSource={RelativeSource AncestorType=ToggleButton}}" 
      PropertyName="Content" Value="Archive project"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</ToggleButton>

2 个答案:

答案 0 :(得分:4)

<ToggleButton Width="50" Height="50">
  <ToggleButton.Content>
        <TextBlock x:Name="obj" Text="Foo"/>
    </ToggleButton.Content>
  <i:Interaction.Triggers>
        <i:EventTrigger EventName="Checked">
            <ei:ChangePropertyAction PropertyName="Text" Value="On" TargetName="obj"/>
        </i:EventTrigger>
        <i:EventTrigger EventName="Unchecked">
            <ei:ChangePropertyAction PropertyName="Text" Value="Off" TargetName="obj"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ToggleButton>

答案 1 :(得分:1)

我最终使用Kent Boogaartconverter,效果很好,并且还依赖于绑定属性,而不依赖于可能根本不会触发的控制触发器(在属性的情况下)实际上没有设置),这是代码:

<ToggleButton.Content>
  <Binding Path="IsArchived"
    xmlns:boo="http://schemas.kent.boogaart.com/converters"
    xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <Binding.Converter>
      <boo:MapConverter>
        <boo:Mapping To="Archive project">
          <boo:Mapping.From>
            <sys:Boolean>false</sys:Boolean>
          </boo:Mapping.From>
        </boo:Mapping>
        <boo:Mapping To="Unarchive project">
          <boo:Mapping.From>
            <sys:Boolean>true</sys:Boolean>
          </boo:Mapping.From>
        </boo:Mapping>
      </boo:MapConverter>
    </Binding.Converter>
  </Binding>
</ToggleButton.Content>