Is it possible using only xaml to change for example Property in style by blend triggers? For example after firing event Checked
on first RadioButton
change property Visibility
in style FirstStyle
on 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>
A Style
object becomes immutable the moment it is applied to a Control
. If this Style
is not applied yet, yes you can change it.
So, you should create 2 separate Styles
and choose a Style
based on Trigger
.
Since, I am not too sure why you want to do only in XAML, but here is my attempt and it works.
I updated your style to the following:
<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>
And then, I set DataContext
on Label
s to the corresponding RadioButton
s. Here is the full 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>
It would have probably been easier if we could bind RadioButton
's IsChecked
and Label
s Visibility to the same property on a backing ViewModel.
Another approach to this problem would be @AnjumSKhan's answer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.