我有两个指向同一TargetType的Style,x:Key分别是A和B。现在有一个TargetType的控件,我想让它在平常态时Style为A,满足某个DataTrigger的条件时Style为B,该如何实现?

解决方案 »

  1.   

    一种是后台写代码来实现,另一种是2个重叠的分别应用2种Style的控件,然后根据DataTrigger设置他们的Visibility。
      

  2.   

    刚试了下。也可以这样写,在外面套层ContentControl然后把你的控件写在Template里面在设置就行。
    如下:<Label >
                <Label.Template>
                    <ControlTemplate>
                        <Label Name="TestLB"/>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="TestLB" Property="Style" Value="{StaticResource NumLabel}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                    
                </Label.Template>
            </Label>
      

  3.   


    谢谢。有一个问题,我用的是DataTrigger,在Template用ElementName获取不到模板外部的控件。请问如何解决?
      

  4.   

    把你的代码贴出这部分来看下。不能把整个都套在这一层ContentControl里么?
      

  5.   

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0" Orientation="Horizontal">
                <Rectangle Width="50" Height="50" Fill="Red" x:Name="rectRed" Visibility="Visible" />
                <Rectangle Width="50" Height="50" Fill="Blue" x:Name="rectBlue" Visibility="Collapsed" />
            </StackPanel>
            <StackPanel Grid.Row="1" Orientation="Horizontal">
                <Button Width="50" Height="50" Content="Red" x:Name="btnRed" />
                <!-- 我想让这个按钮在rectRed的Visibility为Visible时用ActiveStyle,否则用NormanStyle -->
                <Button Width="50" Height="50" Content="Red" x:Name="btnBlue" />
                <!-- 我想让这个按钮在rectBlue的Visibility为Visible时用ActiveStyle,否则用NormanStyle -->
            </StackPanel>
        </Grid>
        <Window.Resources>
            <Style TargetType="Button" x:Key="NormanStyle">
                <Setter Property="Background" Value="Gray" />
            </Style>
            <Style TargetType="Button" x:Key="ActiveStyle">
                <Setter Property="Background" Value="Yellow" />
            </Style>
        </Window.Resources>
    ]
      

  6.   


    <Window.Resources>
            <Style TargetType="Button" x:Key="NormanStyle">
                <Setter Property="Background" Value="Gray" />
            </Style>
            <Style TargetType="Button" x:Key="ActiveStyle">
                <Setter Property="Background" Value="Yellow" />
            </Style>
        </Window.Resources>
        <Grid>
            <Label>
                <Label.Template>
                    <ControlTemplate>
                        <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <StackPanel Grid.Row="0" Orientation="Horizontal">
                            <Rectangle Width="50" Height="50" Fill="Red" x:Name="rectRed" Visibility="Visible" />
                            <Rectangle Width="50" Height="50" Fill="Blue" x:Name="rectBlue" Visibility="Collapsed" />
                        </StackPanel>
                        <StackPanel Grid.Row="1" Orientation="Horizontal">
                                <Button Width="50" Height="50" Content="Red" x:Name="btnRed" Style="{StaticResource NormanStyle}"/>
                            <!-- 我想让这个按钮在rectRed的Visibility为Visible时用ActiveStyle,否则用NormanStyle -->
                                <Button Width="50" Height="50" Content="Red" x:Name="btnBlue" Style="{StaticResource NormanStyle}"/>
                            <!-- 我想让这个按钮在rectBlue的Visibility为Visible时用ActiveStyle,否则用NormanStyle -->
                        </StackPanel>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <DataTrigger Binding="{Binding ElementName=rectRed,Path=Visibility}" Value="Visible">
                                <Setter Property="Style" TargetName="btnRed" Value="{StaticResource ActiveStyle}"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding ElementName=rectBlue,Path=Visibility}" Value="Visible">
                                <Setter Property="Style" TargetName="btnBlue" Value="{StaticResource ActiveStyle}"/>
                            </DataTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Label.Template>
            </Label>
        </Grid>
      

  7.   

    就是在最外层套一个Label之类的ContentControl,然后你的东西全写在里面的Template中,这样触发器就好控制了。
      

  8.   

    数据绑定你想要操作的属性之类的,后台直接操作这些数据一样吧。类似MVVM的开发模式,控件的操作,内容呈现都在后台以修改绑定数据来执行。
    后台想得到控件也是可以的,用GetTemplateChild()方法就是。
      

  9.   

    用FindName()吧,GetTemplateChild()不是public的。
      

  10.   

    把按钮的Visibility和你关心的长方形的Visibility都绑定到同一个属性,直接控制那个属性,当然dataTrigger也可以根据这个属性的Value进行绑定了
      

  11.   

    或者 试试这样   <DataTrigger Binding="{Binding ElementName=rectRed,Path=Visibility}"  Value="Visible">
           .........
       </DataTrigger>
      

  12.   

    我以前遇到过类似问题,我是这样解决的,只写一个style,在datatrigger里重写controltemplate,两种datatrigger,重写两次就行。
    大概是这个意思:                        <Style >
                                <Style.Triggers>
                                    <DataTrigger A>
                                        <Setter Property ="Template">
                                            <Setter.Value>
                                                <ControlTemplate />
                                            </Setter.Value>
                                        </Setter>
                                    </DataTrigger>
                                    <DataTrigger B>
                                        <Setter Property ="Template">
                                            <Setter.Value>
                                                <ControlTemplate />
                                            </Setter.Value>
                                        </Setter>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style >