我是WPF新手,如果这是一个愚蠢的问题,请提前原谅我。如果选中了复选框,我有启用 GroupBox 的语法,效果很好:
IsEnabled="{Binding ElementName=cbIsDeceased, Path=IsChecked}"
但我需要的是翻转极性。当未选中复选框时,我需要 IsEnabled 为真,反之亦然。有没有一种声明性的方式来获得它?
谢谢。
我是WPF新手,如果这是一个愚蠢的问题,请提前原谅我。如果选中了复选框,我有启用 GroupBox 的语法,效果很好:
IsEnabled="{Binding ElementName=cbIsDeceased, Path=IsChecked}"
但我需要的是翻转极性。当未选中复选框时,我需要 IsEnabled 为真,反之亦然。有没有一种声明性的方式来获得它?
谢谢。
您必须添加一个转换器来反转布尔值。在 XAML 中,定义转换器的资源并将其添加到绑定中:
IsEnabled="{Binding ElementName=cbIsDeceased, Path=IsChecked, Converter={StaticResource InverseBooleanConverter}"
为了节省你一些时间,我给你我的转换器版本,它非常简单:)
/// <summary>
/// Converts a boolean to its opposite value
/// </summary>
[ValueConversion(typeof(bool), typeof(bool))]
public class InverseBooleanConverter: IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (targetType != typeof(bool))
throw new InvalidOperationException("The target must be a boolean");
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
#endregion
}
要完全在 XAML 中实现这一点(即没有转换器),您可以使用由 CheckBox 的 Checked 和 Unchecked 事件触发的基于 keframe 的动画。请注意,这不是 Binding 并且没有任何东西“监视” CheckBox 的状态以确定是否应启用 GroupBox。这就是为什么(您将在下面的示例中看到)必须显式设置 GroupBox 和 CheckBox 的初始状态才能使动画正常工作。
这是一个工作示例:
<Window ...>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<GroupBox
x:Name="targetGroupBox"
Grid.Row="0"
Header="My GroupBox"
IsEnabled="False"
>
<Button Content="Hello World" />
</GroupBox>
<CheckBox
Grid.Row="1"
Content="Toggle with animation"
IsChecked="True"
>
<CheckBox.Triggers>
<EventTrigger RoutedEvent="CheckBox.Checked">
<EventTrigger.Actions>
<StopStoryboard BeginStoryboardName="SetFalseStoryboard" />
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="CheckBox.Unchecked">
<EventTrigger.Actions>
<BeginStoryboard Name="SetFalseStoryboard">
<Storyboard>
<BooleanAnimationUsingKeyFrames
Storyboard.TargetName="targetGroupBox"
Storyboard.TargetProperty="IsEnabled"
AutoReverse="True"
BeginTime="0:0:0"
FillBehavior="HoldEnd"
>
<DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:0" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</CheckBox.Triggers>
</CheckBox>
</Grid>
</Window>
显示此视图时,父 GroupBox 中的 Button 仅在未选中“动画”复选框时启用。因为这不是绑定,所以可能需要考虑性能问题,即重复执行的绑定转换器可能比重复“播放”的基于关键帧的动画表现更好;我没有做任何性能测试来验证。
过去我很少使用这样的动画,因为我讨厌编写和引用转换器来实现我认为应该内置到开箱即用的 WPF 中的功能。然而,大多数时候,转换器是最好的选择,这个例子只是为了说明在某些情况下,可以使用基于关键帧的动画完全在 XAML 中实现所需的结果。