[英]WPF Visibility in MultiDataTrigger
为什么当RadioButton1
IsChecked=true
时, <Setter Value="Visible" Property="Control.Visibility"/>
不能使标签可见?
<Window x:Class="WpfApplication9.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="Label" x:Key="Test">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsChecked, ElementName=RadioButton1}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Value="Visible" Property="Control.Visibility"></Setter>
<Setter Property="Background" Value="Red"></Setter>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel>
<RadioButton Content="1" x:Name="RadioButton1" IsChecked="True"></RadioButton>
<RadioButton Content="2" ></RadioButton>
<RadioButton Content="3" x:Name="RadioButton3"></RadioButton>
<RadioButton Content="4"></RadioButton>
<RadioButton Content="5" x:Name="RadioButton5"></RadioButton>
</StackPanel>
<StackPanel Grid.Row="1">
<Label Content="1" Style="{DynamicResource Test}" Visibility="Hidden"></Label>
<Label Content="2" Visibility="Collapsed"></Label>
</StackPanel>
</Grid>
也许您可以尝试在样式中设置标签可见性默认值(隐藏):
<Style TargetType="Label" x:Key="Test">
<Setter Property="Visibility" Value="Hidden"/>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsChecked, ElementName=RadioButton1}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Visibility" Value="Visible"/>
<Setter Property="Background" Value="Red"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
而不是在声明中:
<Label Content="1" Style="{StaticResource Test}"/>
在声明中设置标签可见性时,无法使用样式触发器更改标签的值。
不需要Control.Visibility
,因为您可以指定样式的目标类型。 问题很可能是触发器无法通过元素名称找到绑定。 通常,在视图模型中具有单个属性可以将其绑定到视图中的多个元素会有所帮助。
<Window x:Class="WpfApplication9.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="350"
Width="525"
x:Name="PART_MainWindow">
<Window.Resources>
<Style TargetType="Label" x:Key="Test">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsChecked, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Visibility" Value="Visible"/>
<Setter Property="Background" Value="Red"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid DataContext="{Binding ElementName=PART_MainWindow}">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel>
<RadioButton Content="1" IsChecked="{Binding IsChecked}"/>
<RadioButton Content="2" IsChecked="{Binding IsChecked2}"/>
<RadioButton Content="3" IsChecked="{Binding IsChecked3}"/>
<RadioButton Content="4" IsChecked="{Binding IsChecked4}"/>
<RadioButton Content="5" IsChecked="{Binding IsChecked5}"/>
</StackPanel>
<StackPanel Grid.Row="1">
<Label Content="1" Style="{DynamicResource Test}" Visibility="Hidden"/>
<Label Content="2" Visibility="Collapsed"/>
</StackPanel>
</Grid>
</Window>
笔记:
ResourceDictionary
时,使用ElementName
绑定不能正常工作。 ResourceDictionary
中的绑定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.