从样式中的模板进行BInding

时间:2012-06-28 02:10:05

标签: wpf xaml styles

我正在尝试在下面的Style中找出ControlTemplate.Triggers,我还没想出如何找到命名的Ellipse和Path属性。

例如,当IsMouseOver时,更改Ellipse的背景。找到Ellipse的好方法是什么,所以我可以按照我设置此样式的方式设置Fill属性?有没有更好的方法来解决它?

干杯,
Berryl

<Style x:Key="CloseCrossToggleButtonStyle" TargetType="{x:Type ToggleButton}">

<Setter Property="ContentTemplate">  
    <Setter.Value>
        <DataTemplate>

            <Grid Background="Transparent">

                <!-- The background of the button, as an ellipse. -->
                <Ellipse x:Name="theEllipse" />

                <!-- A path that renders a cross. -->
                <Path x:Name="theCross"...

            </Grid>

        </DataTemplate>
    </Setter.Value>
</Setter>

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type ToggleButton}">
            <ContentPresenter x:Name="theContent"/>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Ellipse.Fill" Value="{StaticResource HoverBackgroundBrush}" />
                    <Setter Property="Path.Stroke" Value="{StaticResource HoverForegroundBrush}"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>

好的,正在工作

 <Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type ToggleButton}">
            <Grid Background="Transparent">
                <!-- The background of the button, as an ellipse. -->
                <Ellipse x:Name="theEllipse" />
                <!-- A path that renders a cross. -->
                <Path x:Name="theCross"
                      ...
                </Path>

            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="theEllipse" Property="Fill" Value="{StaticResource HoverBackgroundBrush}" />
                    <Setter TargetName="theCross" Property="Stroke" Value="{StaticResource HoverForegroundBrush}"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="true">
                    <Setter TargetName="theEllipse" Property="Fill" Value="{StaticResource PressedBackgroundBrush}" />
                    <Setter TargetName="theEllipse" Property="Stroke" Value="{StaticResource PressedBorderBrush}" />
                    <Setter TargetName="theCross" Property="Stroke" Value="{StaticResource PressedForegroundBrush}"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>

1 个答案:

答案 0 :(得分:1)

在ControlTemplate.Triggers中,您只能操作ContentTemplate内部的控件,您无法访问ContentTemplate中的模板元素。

您可以执行以下示例(我正在更改按钮图像...):

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
            <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                <Image x:Name="imgBackground" Source="{StaticResource UpArrowImageNormal}" Stretch="None"/>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsPressed" Value="True">
                    <Setter TargetName="imgBackground"
                            Property="Source" Value="{StaticResource UpArrowImageIsPressed}"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter TargetName="imgBackground" Property="Source" Value="{StaticResource UpArrowImageDisabled}"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>

不幸的是我不知道应用触发器数据模板的方法。 我相信它只能在ContentPresenter中应用触发器......

更正,似乎你可以使用这个(样本):

<Button x:Name="btnTest" Height="23" Width="75"  HorizontalAlignment="Left"  VerticalAlignment="Top" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch">
            <Button.ContentTemplate>
                <DataTemplate>
                    <Rectangle x:Name="t" Fill="Azure" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
                    <DataTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="t" Property="Fill" Value="Black"/>
                        </Trigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </Button.ContentTemplate>
        </Button>