WPF-有没有一种方法可以在ControlTemplate的触发器中定位元素类型? [英] WPF - Is there a way to target an element type in a ControlTemplate's trigger?

查看:84
本文介绍了WPF-有没有一种方法可以在ControlTemplate的触发器中定位元素类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了以下ControlTemplate:

I have the following ControlTemplate defined:

<ControlTemplate x:Key="buttonTemplate" TargetType="{x:Type Button}">
    <Border x:Name="buttonBorder">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <TextBlock x:Name="txtLabel" Grid.Column="0">
                <ContentPresenter/>
            </TextBlock>
            <Canvas x:Name="reschedule" Grid.Column="1">
                <Path x:Name="path1" ... />
                <Path x:Name="path2" ... />
                <Path x:Name="path3" ... />
                <Path x:Name="path4" ... />
                <Path x:Name="path5" ... />
                <Path x:Name="path6" ... />
                <Path x:Name="path7" ... />
                <Path x:Name="path8" ... />
                <Path x:Name="path9" ... />
                <Path x:Name="path10" ... />
            </Canvas>
        </Grid>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter TargetName="buttonBorder" Property="Background" Value="DarkGreen"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter TargetName="buttonBorder" Property="Background" Value="DarkGray"/>
            <Setter TargetName="txtLabel" Property="Foreground" Value="Gray"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

现在,默认的TextBlock ForegroundPath Fill属性已设置为White.禁用按钮后,我要将这些属性设置为Gray.现在它适用于TextBlock,我也可以通过定位它们的每个名称来使其也适用于Path,但是有没有一种方法可以按类型定位所有Path元素呢?像这样:

Right now the default TextBlock Foreground and Path Fill properties have been set to White. When the button is disabled I want to set those properties to Gray. Right now it works for the TextBlock and I can make it work for the Paths too by targetting each of their names, but is there a way to target all the Path elements by type? Something like:

<Setter TargetType="Path" Property="Fill" Value="Gray"/>

我尝试将以下触发器添加到Border元素的样式中,但不起作用:

I've tried adding the following trigger to the Border element's style but it doesn't work:

<Border.Style>
    <Style TargetType="Border">
        <Style.Resources>
            <Style TargetType="Path">
                <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Fill" Value="Gray"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Style.Resources>
    </Style>
</Border.Style>

推荐答案

您可以尝试以下技巧:

Binding创建代理控件:

<Control x:Name="Proxy" Background="White" /> 

并像这样在Path绑定中使用:

And use in Path binding like this:

<Path x:Name="path1" Fill="{Binding Path=Background, ElementName=Proxy}" Data="..." />

当您在触发器"中设置代理的颜色时,他会塞满所有路径".

When you're in the Trigger set the color for the Proxy, his tucked up all the Path's.

或者代替绑定代理,您可以使用任何现有控件,例如TextBlock.

Or instead of the binding Proxy, you can use any existing controls, such TextBlock.

完整示例:

<ControlTemplate x:Key="buttonTemplate" TargetType="{x:Type Button}">
    <Border x:Name="buttonBorder">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>

            <TextBlock x:Name="txtLabel" Grid.Column="0">                        
                <ContentPresenter />
            </TextBlock>

            <Control x:Name="Proxy" Background="White" /> 

            <Canvas x:Name="reschedule" Grid.Column="1">
                <Path x:Name="path1" Fill="{Binding Path=Background, ElementName=Proxy}" Data="..." />
                <Path x:Name="path2" Fill="{Binding Path=Background, ElementName=Proxy}" Data="..." />
            </Canvas>
        </Grid>
    </Border>

    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter TargetName="buttonBorder" Property="Background" Value="DarkGreen" />
        </Trigger>

        <Trigger Property="IsEnabled" Value="False">
            <Setter TargetName="buttonBorder" Property="Background" Value="DarkGray" />
            <Setter TargetName="txtLabel" Property="Foreground" Value="Gray" />
            <Setter TargetName="Proxy" Property="Background" Value="Gray" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

这篇关于WPF-有没有一种方法可以在ControlTemplate的触发器中定位元素类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆