none
如何用一个ChecckBox或RadioButton的IsChecked属性控制触发器触发时所要全部执行项是否执行? RRS feed

  • 问题

  • 在窗体上有一个Button,该button内设置了style(仅考虑<Button.Style></Button.Style>这种style在具体控件对象内的设置情况,不考虑其他情况),里面设置了触发器,有一个触发项是IsMouseOver

    例如设置了鼠标光标悬停到Button上button改变外观的触发项IsMouseOver,使用CheckBox控制这个button上设置的style里的IsMouseOver触发项,CheckBox的IsChecked为true,IsMouseOver触发时改变Button外观,为false时IsMouseOver触发不改变外观,或者说该button的IsMouseOver触发时内里面的所有触发时执行项都不执行。这种如何实现?

    (备注,用button说明,因为这是简化说明)






    2019年1月10日 11:35

答案

  • 重新看了遍依赖属性的优先级 发现代码有问题

    现在重新更新一遍 正确的做法

    <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="391*"/>
                <ColumnDefinition Width="401*"/>
            </Grid.ColumnDefinitions>
            <CheckBox x:Name="CB"  Grid.Column="1" Height="128" Width="128"/>
            <Button Content="Content" Width="75" Height="20" >
                <Button.Style>
                    <Style TargetType="Button">
                        <Style.Triggers>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding IsChecked,ElementName=CB }" Value="True" />
                                    <Condition Binding="{Binding Path=IsMouseOver,RelativeSource={RelativeSource Self} }" Value="True" />
                                </MultiDataTrigger.Conditions>
                                <!--此处为更新Button模板  这个模板和默认模板一样-->
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type Button}">
                                            <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                                <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                            </Border>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                                <!--此处更新你要的Style-->
                                <Setter  Property="Background" Value="Red"/>
                            </MultiDataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
        </Grid>

    ********************此处为错误********************************

    <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="391*"/>
                <ColumnDefinition Width="401*"/>
            </Grid.ColumnDefinitions>
            <CheckBox x:Name="CB" Grid.Column="1" Height="128" Width="128"/>
            <Button Content="Content" Width="75" Height="20">
                <Button.Style>
                    <Style TargetType="Button">
                        <Style.Triggers>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding IsChecked,ElementName=CB }" Value="True" />
                                    <Condition Binding="{Binding Path=IsMouseOver,RelativeSource={RelativeSource Self} }" Value="True" />
                                </MultiDataTrigger.Conditions>
                                <Setter Property="Foreground" Value="Red" />
                            </MultiDataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
        </Grid>



    2019年1月10日 16:03

全部回复

  • 重新看了遍依赖属性的优先级 发现代码有问题

    现在重新更新一遍 正确的做法

    <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="391*"/>
                <ColumnDefinition Width="401*"/>
            </Grid.ColumnDefinitions>
            <CheckBox x:Name="CB"  Grid.Column="1" Height="128" Width="128"/>
            <Button Content="Content" Width="75" Height="20" >
                <Button.Style>
                    <Style TargetType="Button">
                        <Style.Triggers>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding IsChecked,ElementName=CB }" Value="True" />
                                    <Condition Binding="{Binding Path=IsMouseOver,RelativeSource={RelativeSource Self} }" Value="True" />
                                </MultiDataTrigger.Conditions>
                                <!--此处为更新Button模板  这个模板和默认模板一样-->
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type Button}">
                                            <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                                <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                            </Border>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                                <!--此处更新你要的Style-->
                                <Setter  Property="Background" Value="Red"/>
                            </MultiDataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
        </Grid>

    ********************此处为错误********************************

    <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="391*"/>
                <ColumnDefinition Width="401*"/>
            </Grid.ColumnDefinitions>
            <CheckBox x:Name="CB" Grid.Column="1" Height="128" Width="128"/>
            <Button Content="Content" Width="75" Height="20">
                <Button.Style>
                    <Style TargetType="Button">
                        <Style.Triggers>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding IsChecked,ElementName=CB }" Value="True" />
                                    <Condition Binding="{Binding Path=IsMouseOver,RelativeSource={RelativeSource Self} }" Value="True" />
                                </MultiDataTrigger.Conditions>
                                <Setter Property="Foreground" Value="Red" />
                            </MultiDataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
        </Grid>



    2019年1月10日 16:03
  • Hello,

    根据你的描述, 你是想把DataTrigger和Trigger组合起来一起用,要想达到你的要求, 你可以使用MultiDataTrigger,请参考下面的例子:

     <StackPanel Orientation="Horizontal">
                <Button
                    Name="btn1"
                    Width="200"
                    Height="30"
                    Margin="10"
                    Content="btn1">
                    <Button.Style>
                        <Style TargetType="{x:Type Button}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type Button}">
                                        <Border Background="{TemplateBinding Background}">
                                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                                        </Border>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                            <Style.Triggers>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding ElementName=checkbox1, Path=IsChecked}" Value="true" />
                                        <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsMouseOver}" Value="true" />
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Background" Value="Red" />
                                </MultiDataTrigger>
    
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                </Button>
                <CheckBox Name="checkbox1" Content="select" HorizontalAlignment="Center" VerticalAlignment="Center" />
            </StackPanel>

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年1月11日 6:19
    版主