简体   繁体   English

使用带有父级属性的触发器

[英]Using trigger with property from parent

I have a Grid with a ToggleButton.我有一个带有 ToggleButton 的网格。 The ToggleButton has a ControlTemplate and in this ControlTemplate I've defined a Ellipse and a Path. ToggleButton 有一个 ControlTemplate,在这个 ControlTemplate 中我定义了一个 Ellipse 和一个 Path。 Atm the color on the Ellipse and the Path is changeing if the ToggleButtons IsMouseOver property is true.如果 ToggleButtons IsMouseOver 属性为真,则椭圆和路径上的颜色会发生变化。 Now I want to change the color if the Grids IsMouseOver property is true as well.现在,如果 Grids IsMouseOver 属性也为 true,我想更改颜色。 But I can't get it working.但我无法让它工作。

My code looks like我的代码看起来像

                <Grid Height="26"
    x:Name="GroupboxHeader"
    Background="Blue">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <ToggleButton Grid.Column="1"
                HorizontalAlignment="Right"
                Margin="0 0 5 0"
                OverridesDefaultStyle="True"
                Background="LightGray"
                Height="19"
                Width="19">
        <ToggleButton.Template>
            <ControlTemplate>
                <Grid>
                    <Grid.LayoutTransform>
                        <TransformGroup>
                            <TransformGroup.Children>
                                <TransformCollection>
                                    <RotateTransform Angle="90" />
                                </TransformCollection>
                            </TransformGroup.Children>
                        </TransformGroup>
                    </Grid.LayoutTransform>
                    <Ellipse x:Name="circle"
                            HorizontalAlignment="Center"
                            Height="19"
                            Stroke="White"
                            VerticalAlignment="Center"
                            Width="19" />
                    <Path x:Name="arrow"
                        Data="M 1,1.5 L 4.5,5 L 8,1.5"
                        HorizontalAlignment="Center"
                        SnapsToDevicePixels="false"
                        Stroke="White"
                        StrokeThickness="2"
                        VerticalAlignment="Center" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver"
                            Value="true">
                        <Setter Property="Stroke"
                                TargetName="circle"
                                Value="LightGray" />
                        <Setter Property="Stroke"
                                TargetName="arrow"
                                Value="LightGray" />
                    </Trigger>
                    <DataTrigger Binding="{Binding ElementName=GroupboxHeader, Path=IsMouseOver}">
                        <Setter Property="Stroke"
                                TargetName="circle"
                                Value="LightGray" />
                        <Setter Property="Stroke"
                                TargetName="arrow"
                                Value="LightGray" />
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </ToggleButton.Template>
    </ToggleButton>
</Grid>

Change your ellipse/path stroke to {TemplateBinding BorderBrush} , and instead of using TargetName in your triggers change BorderBrush on your button.将椭圆/路径笔触更改为{TemplateBinding BorderBrush} ,而不是在触发器中使用TargetName更改按钮上的BorderBrush This way you can add triggers on grid with TargetName set to your toggle button to achieve desired behavior.通过这种方式,您可以在将TargetName设置为切换按钮的网格上添加触发器以实现所需的行为。

EDIT:编辑:

As this is not in control template, TargetName would not find child elements.由于这不在控制模板中, TargetName将找不到子元素。 Instead, you can set triggers to:相反,您可以将触发器设置为:

<DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource FindAncestor, AncestorType=Grid}}" Value="True">

inside your ToggleButton style.在您的ToggleButton样式中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM