【发布时间】:2016-09-23 07:32:02
【问题描述】:
是否可以仅使用 xaml 通过混合触发器更改样式属性?例如,在第一个RadioButton 上触发事件Checked 之后,在Visible 上更改Visibility 样式的FirstStyle 属性。
<Window x:Class="switch_style.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:Interactions="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="FirstStyle" TargetType="Label" x:Name="block">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
</Window.Resources>
<Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Label Style="{DynamicResource FirstStyle}">First</Label>
<Label>Second</Label>
<Label>Third</Label>
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Vertical">
<RadioButton Width="60" HorizontalAlignment="Left" Content="First">
<Interactivity:Interaction.Triggers>
<Interactivity:EventTrigger EventName="Checked">
<Interactions:ChangePropertyAction TargetName="{Binding block}" PropertyName="Visibility" Value="Visible"/>
</Interactivity:EventTrigger>
</Interactivity:Interaction.Triggers>
</RadioButton>
<RadioButton Width="60" HorizontalAlignment="Left">Second</RadioButton>
<RadioButton Width="60" HorizontalAlignment="Left">Third</RadioButton>
</StackPanel>
</Grid>
</Grid>
</Window>
【问题讨论】:
-
如果您试图根据 RadioButton 是否被选中来使标签可见/不可见,那么为什么不直接将 Label 的可见性绑定到 RadioButton IsChecked 属性呢?
-
@sthotakura,也许这是个好主意。如果我应该使用一种样式并且在其中我应该绑定标签的可见性以检查按钮中的属性。但是如何将样式中的 Value={Binding ...} 绑定到控件属性?
-
嗯.. 这很棘手。请看我的回答。