Setter.TargetName不反对从支持算法FMP设置一个元素的工作 [英] Setter.TargetName does not work against an element from the BasedOn setting
问题描述
我有画各种图形的不同按钮许多的PathGeometry实例。所以,我创建了一个按钮类型呈现出路径,然后根据更新的按钮状态Path.Stroke。因此表明,当鼠标滑过它变灰当残疾人和不同的颜色。标准的东西...
I have many PathGeometry instances that draw a variety of graphics for different buttons. So I have created a button type for showing a Path which then updates the Path.Stroke depending on the button state. So show it grayed when disabled and different colours when the mouse is over. Standard stuff...
<Style x:Key="BasePathButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="Border"
<Path x:Name="Path" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Path" Property="Stroke" Value="Gray"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Path" Property="Stroke" Value="Green"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Path" Property="Stroke" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
但显然我需要为每个按钮实例不同Path.Data。所以,我创建了一个支持算法FMP样式设置Path.Data像这样...
But obviously I need different Path.Data for each button instance. So I create a BasedOn style for setting the Path.Data like this...
<Style x:Key="NLB" TargetType="{x:Type Button}" BasedOn="{StaticResource BasePathButton}">
<Setter TargetName="Path" Property="Data" Value="{StaticResource LeftPathGeometry}"/>
</Style>
...但是这未能与的TargetName =路径无法找到一个错误。任何想法如何解决这一问题?或者,也许有更好的方式来创建具有与几何参数使用的路径的按钮?
...but this fails with an error that TargetName="Path" cannot be found. Any ideas how to fix this? Or maybe a better way to create a button that has a Path which is parameterized with the geometry to use?
推荐答案
您不能定位在通过名称的模板元素,这是一个不同的名称范围,此外我认为的TargetName
只工作在 ControlTemplate.Triggers
。你可以参考的数据为 DynamicResource
,然后将资源添加到您的个人风格。
You cannot target elements in the template via name, it's a different namescope, also i think TargetName
only works in ControlTemplate.Triggers
. You could reference the data as DynamicResource
and then add the resource to your individual styles.
例如
<Path Data="{DynamicResource PathData}" .../>
<Style x:Key="..." BasedOn="...">
<Style.Resources>
<!-- not sure if this actually works, you could also try DynamicResource here -->
<StaticResource x:Key="PathData" ResourceKey="LeftPathGeometry"/>
</Style.Resources>
</Style>
下面是一个完全独立的例子说明了这种方法:
Here is a full standalone example showing this technique:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<SolidColorBrush x:Key="RedBrush" Color="Red"/>
<SolidColorBrush x:Key="BlueBrush" Color="Blue"/>
<Style x:Key="BaseStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderBrush="{DynamicResource StyleBrush}" BorderThickness="3" Padding="5">
<ContentPresenter TextElement.Foreground="{DynamicResource StyleBrush}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="RedStyle" BasedOn="{StaticResource BaseStyle}" TargetType="Button">
<Style.Resources>
<StaticResource x:Key="StyleBrush" ResourceKey="RedBrush"/>
</Style.Resources>
</Style>
<Style x:Key="BlueStyle" BasedOn="{StaticResource BaseStyle}" TargetType="Button">
<Style.Resources>
<StaticResource x:Key="StyleBrush" ResourceKey="BlueBrush"/>
</Style.Resources>
</Style>
</Page.Resources>
<StackPanel>
<Button Content="Test" Style="{StaticResource RedStyle}"/>
<Button Content="Test" Style="{StaticResource BlueStyle}"/>
</StackPanel>
</Page>
这篇关于Setter.TargetName不反对从支持算法FMP设置一个元素的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!