我有一些视觉元素的定义,我想要用于两个不同的控件(按钮,拇指)。有没有办法让二人摆脱重复的代码?
<Style x:Key="HorizontalSliderThumbStyle" TargetType="{x:Type Thumb}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
this block is the same --> <Grid>
<VisualStateManager.VisualStateGroups>
...
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="borderRect" Fill="{TemplateBinding BorderBrush}" />
...
<Style x:Key="KeyboardButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
this block is the same --> <Grid>
<VisualStateManager.VisualStateGroups>
...
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="borderRect" Fill="{TemplateBinding BorderBrush}" />
...发布于 2015-06-19 09:45:00
你可以这样做:
<ControlTemplate TargetType="{x:Type Control}" x:Key="SharedControlTemplate">
<Grid>
<Rectangle Fill="{TemplateBinding BorderBrush}"/>
<TextBlock Text="Shared Control Template"/>
</Grid>
</ControlTemplate>
<Style TargetType="{x:Type Thumb}" x:Key="HorizontalSliderThumbStyle">
<Setter Property="Template" Value="{StaticResource SharedControlTemplate}"/>
</Style>
<Style TargetType="{x:Type Button}" x:Key="KeyboardButtonStyle">
<Setter Property="Template" Value="{StaticResource SharedControlTemplate}"/>
</Style>Thumb和Button都来自Control的基类(不是立即,但它是祖先之一)。因此,如果为其定义了一个ControlTemplate,则可以为两个控件提供相同的模板。然后,在风格上,你会定制他们的个人行为,如果这是你需要的。
使用示例:
<Thumb Style="{StaticResource HorizontalSliderThumbStyle}" BorderBrush="AliceBlue"/>
<Button Style="{StaticResource KeyboardButtonStyle}" BorderBrush="Aquamarine"/>

编辑:
在您提到的注释中,您希望在ContentPresenter中使用ControlTemplate。您必须绑定到Content属性的ContentControl。因为不是每个Control都是ContentControl,所以它只适用于那些包含实际内容的Controls .但是如果它没有内容,就不会发生错误。这是零钱:
<ControlTemplate TargetType="{x:Type Control}" x:Key="SharedControlTemplate">
<Grid>
<Rectangle Fill="{TemplateBinding BorderBrush}"/>
<TextBlock Text="Shared Control Template"/>
<ContentPresenter Content="{TemplateBinding ContentControl.Content}"/>
</Grid>
</ControlTemplate>注意,我为绑定到ContentPresenter内容的ContentControl添加了一行。如果您想添加某种内容,它将接受它:
<Thumb Style="{StaticResource HorizontalSliderThumbStyle}" BorderBrush="AliceBlue"/>
<Button Style="{StaticResource KeyboardButtonStyle}" BorderBrush="Aquamarine">
<Rectangle Fill="Red" Width="100" Height="20"/>
</Button>

发布于 2015-06-19 09:35:30
您应该能够使用DataTemplate来定义内部控件,然后使用 Class在每个ControlTemplate中呈现它们。试试这个:
在Resources中
<DataTemplate x:Key="InnerContent">
<!-- Define your inner content here -->
</DataTemplate>..。
<ControlTemplate TargetType="{x:Type Thumb}">
<ContentControl Content="{Binding}" ContentTemplate="{StaticResource InnerContent}" />
</ControlTemplate>..。
<ControlTemplate TargetType="{x:Type Button}">
<ContentControl Content="{Binding}" ContentTemplate="{StaticResource InnerContent}" />
</ControlTemplate>我现在无法检查,但如果不起作用,设置相同属性的ContentPresenter应该会检查。
发布于 2015-06-19 09:54:41
您可以创建基本样式。
<Style x:Key="BaseStyle" TargetType="Control">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Control">
...
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>并使用BasedOn派生您的样式。
<Style x:Key="ButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseStyle}">
...
</Style>https://stackoverflow.com/questions/30934473
复制相似问题