使ToggleButton
内容取决于其检查状态的最短的xamly方法是什么?
在WPF中,我可能会选择在Silverlight中不存在的DataTrigger
。
我尝试了以下操作,但是它不起作用,一旦包含触发器,与源的绑定(bind)就会中断。触发器将无法正常工作。
<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>
最佳答案
<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>
关于silverlight - ToggleButton/CheckBox的内容取决于其检查状态?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9986727/