Wpf 如何在没有自定义控件的情况下在xaml中创建参数化样式
我有两种风格,几乎完全相同:Wpf 如何在没有自定义控件的情况下在xaml中创建参数化样式,wpf,xaml,Wpf,Xaml,我有两种风格,几乎完全相同: <Style x:Key="CancelButtonStyle" TargetType="{x:Type Button}" > <!-- gigantic common code --> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <!
<Style x:Key="CancelButtonStyle" TargetType="{x:Type Button}" >
<!-- gigantic common code -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<!-- more common code -->
<Path Data="DIFFERENT_VALUE_A"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="OkButtonStyle" TargetType="{x:Type Button}" >
<!-- gigantic common code -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<!-- more common code -->
<Path Data="DIFFERENT_VALUE_B"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我想提取通用代码(“样式模板”),然后像这样使用它:
<Style x:Key="OkButtonStyle" Base="PathButtonStyle" PathData="DIFFERENT_VALUE_B" />
编辑:
正如我在标题中指定的,我不想创建自己的控件您可以创建自己的按钮,该按钮派生自Wpf按钮,并具有属性PathData:
public class PathButton:Button
{
public string PathData {get;set;}
}
<Style x:Key="BaseStyle" TargetType="{x:Type Button}" >
<!-- gigantic common code -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<!-- more common code -->
<Path Data="{TemplateBinding Tag}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
您的风格可能是:
<Style x:Key="PathButtonStyle" TargetType="{x:Type ns:PathButton}" >
<!-- gigantic common code -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<!-- more common code -->
<Path Data="{TemplateBinding PathData}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
要使用它,您必须执行以下操作:
<ns:PathButton Style="{StaticResource PathButtonStyle}" PathData="M200 200 100 100 L0 0"/>
或者创建自己的自定义控件
,方法相同
这里的冒险之处在于,无论您想使用多少个不同的按钮,您的资源中始终只有一个Style
和一个类(PathButton
)。
您只需更改
PathButton
的PathData
-属性,即可更改其外观。您可以创建自己的按钮,该按钮源自Wpf按钮,具有以下属性PathData:
public class PathButton:Button
{
public string PathData {get;set;}
}
<Style x:Key="BaseStyle" TargetType="{x:Type Button}" >
<!-- gigantic common code -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<!-- more common code -->
<Path Data="{TemplateBinding Tag}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
您的风格可能是:
<Style x:Key="PathButtonStyle" TargetType="{x:Type ns:PathButton}" >
<!-- gigantic common code -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<!-- more common code -->
<Path Data="{TemplateBinding PathData}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
要使用它,您必须执行以下操作:
<ns:PathButton Style="{StaticResource PathButtonStyle}" PathData="M200 200 100 100 L0 0"/>
或者创建自己的自定义控件
,方法相同
这里的冒险之处在于,无论您想使用多少个不同的按钮,您的资源中始终只有一个Style
和一个类(PathButton
)。
您只需更改
PathButton的PathData
-属性即可更改其外观。只需在参考资料中分别定义控件模板
对象即可:
<ControlTemplate x:Key="ControlTemplate1">
<!-- different part -->
</ControlTemplate>
<ControlTemplate x:Key="ControlTemplate2">
<!-- different part -->
</ControlTemplate>
然后,参照新的ControlTemplate
s,将新样式建立在该样式的基础上:
<Style x:Key="CancelButtonStyle" TargetType="{x:Type Button}"
BasedOn="{StaticResource BaseStyle}">
<Setter Property="Template" Value="{StaticResource ControlTemplate1}" />
</Style>
<Style x:Key="OkButtonStyle" TargetType="{x:Type Button}"
BasedOn="{StaticResource BaseStyle}">
<Setter Property="Template" Value="{StaticResource ControlTemplate2}" />
</Style>
只需在资源
中分别定义控制模板
对象即可:
<ControlTemplate x:Key="ControlTemplate1">
<!-- different part -->
</ControlTemplate>
<ControlTemplate x:Key="ControlTemplate2">
<!-- different part -->
</ControlTemplate>
然后,参照新的ControlTemplate
s,将新样式建立在该样式的基础上:
<Style x:Key="CancelButtonStyle" TargetType="{x:Type Button}"
BasedOn="{StaticResource BaseStyle}">
<Setter Property="Template" Value="{StaticResource ControlTemplate1}" />
</Style>
<Style x:Key="OkButtonStyle" TargetType="{x:Type Button}"
BasedOn="{StaticResource BaseStyle}">
<Setter Property="Template" Value="{StaticResource ControlTemplate2}" />
</Style>
您可以使用标记传递路径数据:
public class PathButton:Button
{
public string PathData {get;set;}
}
<Style x:Key="BaseStyle" TargetType="{x:Type Button}" >
<!-- gigantic common code -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<!-- more common code -->
<Path Data="{TemplateBinding Tag}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
然后以个人样式设置标记:
<Style x:Key="CancelButtonStyle" TargetType="{x:Type Button}"
BasedOn="{StaticResource BaseStyle}">
<Setter Property="Tag" Value="MY PAth A" />
</Style>
<Style x:Key="OkButtonStyle" TargetType="{x:Type Button}"
BasedOn="{StaticResource BaseStyle}">
<Setter Property="Tag" Value="MY PAth B" />
</Style>
您可以使用标记传递路径数据:
public class PathButton:Button
{
public string PathData {get;set;}
}
<Style x:Key="BaseStyle" TargetType="{x:Type Button}" >
<!-- gigantic common code -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<!-- more common code -->
<Path Data="{TemplateBinding Tag}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
然后以个人样式设置标记:
<Style x:Key="CancelButtonStyle" TargetType="{x:Type Button}"
BasedOn="{StaticResource BaseStyle}">
<Setter Property="Tag" Value="MY PAth A" />
</Style>
<Style x:Key="OkButtonStyle" TargetType="{x:Type Button}"
BasedOn="{StaticResource BaseStyle}">
<Setter Property="Tag" Value="MY PAth B" />
</Style>
正如我在标题中指定的,我不想创建任何自定义控件(包括自定义按钮)Sry,我没有读到:D…为什么不想使用自己的控件?正如我在标题中指定的,我不想创建任何自定义控件(包括自定义按钮)Sry,我没有读到:D…你为什么不想使用自己的控件?这比另一种方法的代码要少。这比完全重复要好,但我还是在ControlTemplate中找到了很多常用代码(“我的问题中的“更常用的代码”),我让我的示例更清楚了。您的通用代码只编写一次,并且只有不同的部分单独编写。。。就像你问的。编辑>>哦,我以前没有看到您的代码更改,这比完全复制要好,但我最终还是在ControlTemplate中找到了很多常见代码(“我的问题中的“更常见的代码”),我让我的示例更清晰了。您的通用代码只编写一次,并且只有不同的部分单独编写。。。就像你问的。编辑>>哦,在代码变脏之前,我没有看到您的代码更改,但符合我的条件(没有自定义控件),我同意它有点脏。另一种方法是执行相同的操作,但不使用标记,而是使用您创建的附加属性。这会更“干净”,但需要您创建附加属性…它是脏的,但符合我的条件(没有自定义控件)。我同意它有点脏。另一种方法是执行相同的操作,但不使用标记,而是使用您创建的附加属性。这会更“干净”,但需要您创建附加的属性。。。。