WPF - 有没有办法在ControlTemplate的触发器中定位元素类型?

时间:2015-01-19 21:45:44

标签: wpf xaml triggers controltemplate targettype

我定义了以下ControlTemplate:

<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元素?类似的东西:

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

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

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

3 个答案:

答案 0 :(得分:2)

你可以尝试这个技巧:

Binding创建代理控制:

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

并在Path绑定中使用:

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

当您在触发器中设置代理的颜色时,他隐藏了所有路径。

或者代替绑定代理,您可以使用任何现有控件,例如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>

答案 1 :(得分:1)

1)使用 Canvas资源来存储路径样式。

请参阅 <Trigger Property="IsEnabled" Value="False"> <Trigger Property="IsEnabled" Value="True">

 <Window.Resources>
    <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" Data="M 0 0 L 0 10 L 10 10 Z"/>
                    <Path x:Name="path2"  Data="M 0 0 L 0 10 L 10 10 Z" />                     
                </Canvas>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>                                            
            <Trigger Property="IsEnabled" Value="False">
                <Setter TargetName="buttonBorder" Property="Background" Value="DarkGray"/>
                <Setter TargetName="reschedule" Property="Style">
                    <Setter.Value>
                        <Style TargetType="{x:Type Canvas}">
                            <Style.Resources>
                                <Style TargetType="{x:Type Path}">
                                    <Setter Property="Fill" Value="Green"></Setter>
                                </Style>
                            </Style.Resources>
                        </Style>
                    </Setter.Value>
                </Setter>
            </Trigger>                                       
            <Trigger Property="IsEnabled" Value="True">                 
                <Setter TargetName="reschedule" Property="Style">
                    <Setter.Value>
                        <Style TargetType="{x:Type Canvas}">
                            <Style.Resources>
                                <Style TargetType="{x:Type Path}">
                                    <Setter Property="Fill" Value="Blue"></Setter>
                                </Style>
                            </Style.Resources>
                        </Style>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>       
</Window.Resources>

<StackPanel>
    <Button Height="40" Width="40" Template="{StaticResource buttonTemplate}" IsEnabled="False"></Button>
    <Button Height="40" Width="40" Margin="10" Template="{StaticResource buttonTemplate}" IsEnabled="True"></Button>
</StackPanel>

2)使用画布标记

  <Window.Resources>
    <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" Tag="Red" Grid.Column="1">
                    <Path x:Name="path1" Fill="{Binding Path=Tag,RelativeSource={RelativeSource AncestorType={x:Type Canvas}}}" Data="M 0 0 L 0 10 L 10 10 Z"/>
                    <Path x:Name="path2" Fill="{Binding Path=Tag,RelativeSource={RelativeSource AncestorType={x:Type Canvas}}}"  Data="M 0 0 L 0 10 L 10 10 Z" />
                </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="reschedule" Property="Tag" Value="Green"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Window.Resources>

<StackPanel>
    <Button Height="40" Width="40" Template="{StaticResource buttonTemplate}" IsEnabled="False"></Button>
    <Button Height="40" Width="40" Margin="10" Template="{StaticResource buttonTemplate}" IsEnabled="True"></Button>
</StackPanel>

答案 2 :(得分:0)

你的风格不起作用的原因是因为它在模板内部 要使其工作,您需要在模板之外应用您的样式。不知道为什么这样做,可能与在xaml中处理样式的方式有关。
另一个需要讨论的问题是风格定义的重要性:
<Style TargetType="Path"><Style TargetType="{x:Type Path}">不同 如果在资源标记中定义,第一个将给出错误,因为它需要一个键,您可以通过该键在所有目标类型中明确引用样式。
后者被分配给Path类型的每个控件,因此如果您在DockPanel内定义它,那么Path内的每个DockPanel都会受到样式的影响,但是如果Path位于DockPanel之外,不会应用任何样式,除非在别处明确定义,否则此样式将隐式应用于控件。
HTH