为什么<Setter Value="Visible" Property="Control.Visibility"/>在RadioButton1 IsChecked=true时不使标签可见
<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>
发布于 2016-10-08 18:44:14
也许您可以尝试在样式中设置标签可见性默认值(Hidden):
<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}"/>当您在声明中设置lable visibilty时,lable的值不能使用样式触发器进行更改。
发布于 2016-10-08 18:55:11
不需要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>备注:
ElementName中时,使用ResourceDictionary的绑定不能正常工作。ResourceDictionary中的绑定。https://stackoverflow.com/questions/39935871
复制相似问题