首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对两个ControlTemplates使用相同的定义

对两个ControlTemplates使用相同的定义
EN

Stack Overflow用户
提问于 2015-06-19 09:22:19
回答 3查看 48关注 0票数 4

我有一些视觉元素的定义,我想要用于两个不同的控件(按钮,拇指)。有没有办法让二人摆脱重复的代码?

代码语言:javascript
复制
   <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}" />
                                   ...
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-06-19 09:45:00

你可以这样做:

代码语言:javascript
复制
<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>

ThumbButton都来自Control的基类(不是立即,但它是祖先之一)。因此,如果为其定义了一个ControlTemplate,则可以为两个控件提供相同的模板。然后,在风格上,你会定制他们的个人行为,如果这是你需要的。

使用示例:

代码语言:javascript
复制
<Thumb Style="{StaticResource HorizontalSliderThumbStyle}" BorderBrush="AliceBlue"/>
<Button Style="{StaticResource KeyboardButtonStyle}" BorderBrush="Aquamarine"/>

编辑:

在您提到的注释中,您希望在ContentPresenter中使用ControlTemplate。您必须绑定到Content属性的ContentControl。因为不是每个Control都是ContentControl,所以它只适用于那些包含实际内容的Controls .但是如果它没有内容,就不会发生错误。这是零钱:

代码语言:javascript
复制
<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添加了一行。如果您想添加某种内容,它将接受它:

代码语言:javascript
复制
<Thumb Style="{StaticResource HorizontalSliderThumbStyle}" BorderBrush="AliceBlue"/>
<Button Style="{StaticResource KeyboardButtonStyle}" BorderBrush="Aquamarine">
    <Rectangle Fill="Red" Width="100" Height="20"/>
</Button>

票数 2
EN

Stack Overflow用户

发布于 2015-06-19 09:35:30

您应该能够使用DataTemplate来定义内部控件,然后使用 Class在每个ControlTemplate中呈现它们。试试这个:

Resources

代码语言:javascript
复制
<DataTemplate x:Key="InnerContent">
    <!-- Define your inner content here -->
</DataTemplate>

..。

代码语言:javascript
复制
<ControlTemplate TargetType="{x:Type Thumb}">
    <ContentControl Content="{Binding}" ContentTemplate="{StaticResource InnerContent}" />
</ControlTemplate>

..。

代码语言:javascript
复制
<ControlTemplate TargetType="{x:Type Button}">
    <ContentControl Content="{Binding}" ContentTemplate="{StaticResource InnerContent}" />
</ControlTemplate>

我现在无法检查,但如果不起作用,设置相同属性的ContentPresenter应该会检查。

票数 0
EN

Stack Overflow用户

发布于 2015-06-19 09:54:41

您可以创建基本样式。

代码语言:javascript
复制
<Style x:Key="BaseStyle" TargetType="Control">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Control">
                 ...
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

并使用BasedOn派生您的样式。

代码语言:javascript
复制
<Style x:Key="ButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseStyle}">
        ...        
</Style>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30934473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档