使用来自父级的属性的触发器

时间:2014-05-14 09:18:15

标签: wpf triggers grid ismouseover

我有一个带有ToggleButton的网格。 ToggleButton有一个ControlTemplate,在这个ControlTemplate中我定义了一个Ellipse和一个Path。如果ToggleButtons IsMouseOver属性为true,则椭圆上的颜色和路径正在更改。现在,如果Grids IsMouseOver属性为true,我想更改颜色。但我无法让它发挥作用。

我的代码看起来像

                <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>

1 个答案:

答案 0 :(得分:2)

将椭圆/路径笔划更改为{TemplateBinding BorderBrush},而不是在触发器中使用TargetName更改按钮上的BorderBrush。这样,您可以在网格上添加触发器,并将TargetName设置为切换按钮,以实现所需的行为。

修改

由于这不在控件模板中,TargetName将找不到子元素。相反,您可以在<DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource FindAncestor, AncestorType=Grid}}" Value="True">样式中将触发器设置为:ToggleButton