WPF使用混合

时间:2016-09-23 07:32:03

标签: c# wpf xaml blend

是否可以仅使用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>

2 个答案:

答案 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的答案。