是否可以仅使用xaml来更改混合触发器的样式属性?例如,在Checked
上的RadioButton
样式的第一个Visibility
更改属性FirstStyle
上触发事件Visible
之后。
<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>
答案 0 :(得分:1)
Style
对象在应用于Control
时变为不可变。如果尚未应用此Style
,则可以更改它。
因此,您应创建2个单独的Styles
并根据Style
选择Trigger
。
答案 1 :(得分:1)
因为,我不太清楚为什么你只想在XAML中做,但这是我的尝试并且有效。
我将您的风格更新为以下内容:
<Style x:Key="FirstStyle" TargetType="Label">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
然后,我将DataContext
上的Label
设置为相应的RadioButton
。这是完整的XAML:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<Style x:Key="FirstStyle" TargetType="Label">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Page.Resources>
<Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Label Content="First" DataContext="{Binding ElementName=radioButton1}" Style="{DynamicResource FirstStyle}"/>
<Label Content="Second" DataContext="{Binding ElementName=radioButton2}" Style="{DynamicResource FirstStyle}"/>
<Label Content="Third" DataContext="{Binding ElementName=radioButton3}" Style="{DynamicResource FirstStyle}"/>
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Vertical">
<RadioButton
x:Name="radioButton1"
Width="60"
HorizontalAlignment="Left"
Content="First"/>
<RadioButton
x:Name="radioButton2"
Width="60"
HorizontalAlignment="Left"
Content="Second"/>
<RadioButton
x:Name="radioButton3"
Width="60"
HorizontalAlignment="Left"
Content="Third"/>
</StackPanel>
</Grid>
</Grid>
</Page>
如果我们可以将RadioButton
的{{1}}和IsChecked
的可见性绑定到支持ViewModel上的同一属性,那可能会更容易。
解决这个问题的另一种方法是@ AnjumSKhan的答案。