我有以下情形:

[TemplatePart(Name = GoToEditModeButtonPart, Type = typeof(DoubleClickButton))]
public class ValueBoxWithLabel : ContentControl
{
    public const string GoToEditModeButtonPart = "GoToEditModeButtonPart";

    #region LabelText Dependency Property ...

    #region IsInEditMode Dependency Property ...

    public event EventHandler<ModeChangedEventArgs> ModeChanged;

    public ValueBoxWithLabel()
    {
        DefaultStyleKey = typeof (ValueBoxWithLabel);
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        //IsInEditMode invokes ModeChanged in the dependency property
        ((DoubleClickButton) GetTemplateChild(GoToEditModeButtonPart)).DoubleClick += (sender, args) => IsInEditMode = true;
    }

    private void InvokeModeChanged(ModeChangedEventArgs e)
    {
        EventHandler<ModeChangedEventArgs> mode = ModeChanged;
        if (mode != null)
            mode(this, e);
    }
}

ValueBox是必不可少的任何输入框的面板。现在简单,但整个应用程序将被重复使用,并且将包含更复杂的behavoir和布局。

文本框作为输入是所使用的必须的,因此我使这个控制:

public class TextBoxWithLabel : ValueBoxWithLabel
{
    #region Text Dependency Property ...

    public TextBoxWithLabel()
    {
        DefaultStyleKey = typeof (TextBoxWithLabel);
    }
}

然后我有电流generic.xaml,它不工作,但它的想法让我想要什么:

<ResourceDictionary>

<ControlTemplate x:Key="ValueBoxWithLabelTemplate">
    <StackPanel Style="{StaticResource ValueBoxWithLabelPanelStyle}">
        <TextBlock Style="{StaticResource LabelStyle}" Text="{TemplateBinding LabelText}" />
        <Grid>
            <ContentPresenter Content="{TemplateBinding Content}" />
            <local:DoubleClickButton Background="Black" x:Name="GoToEditModeButtonPart"></local:DoubleClickButton>
        </Grid>
    </StackPanel>
</ControlTemplate>

<Style TargetType="local:ValueBoxWithLabel">
    <Setter Property="Template" Value="{StaticResource ValueBoxWithLabelTemplate}" />
</Style>

<Style TargetType="local:TextBoxWithLabel">
    <Setter Property="Template" Value="{StaticResource ValueBoxWithLabelTemplate}" />
    <Setter Property="Content">
        <Setter.Value>
            <TextBox Style="{StaticResource ValueBoxStyle}" Text="{TemplateBinding Text}" />
        </Setter.Value>
    </Setter>
</Style>

由于ValueBoxWithLabel是最常用的一个文本框,我想弥补这方面的控制,其重复使用相同的模板,所以我不需要复制/粘贴模板,并有保持的headace既跟上时代的具有相同的变化。

如何重用ValueBoxWithLabelTemplate,只覆盖内容性质一致的模板的休息吗?

有帮助吗?

解决方案

它的一个有趣的方法。我还没有尝试过自己,但看起来这种方法可能会奏效。

您目前的问题是,你要使用的ContentContentPresenter财产。然而,需要被分配控制,在这种情况下,你是一个TextBox做的一个具体实例。因为它不是一个TemplateBinding的一部分不能使用TextBoxControlTemplate

即使没有TemplateBinding问题,你只能够与无论如何它创建一个控制,因为你不能“再利用”一TextBox的同一实例在多个地方。这就是为什么我们在首位的模板。

<强>模板一路

解决模板问题应该不是很难。真正欺骗的事情是找到专门控制的内部控制内容的专门类的属性结合的方式。这是很难,所以当你不能使用TemplateBinding

首先,我将列出的是至少需要为了得到渲染的控件进行更改: -

<ControlTemplate x:Key="ValueBoxWithLabelTemplate">
    <StackPanel Style="{StaticResource ValueBoxWithLabelPanelStyle}">
        <TextBlock Style="{StaticResource LabelStyle}" Text="{TemplateBinding LabelText}" />
        <Grid>
            <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" />
            <local:DoubleClickButton Background="Black" x:Name="GoToEditModeButtonPart"></local:DoubleClickButton>
        </Grid>
    </StackPanel>
</ControlTemplate>

TextBoxWithLabel变为: -

<Style TargetType="local:TextBoxWithLabel">
    <Setter Property="Template" Value="{StaticResource ValueBoxWithLabelTemplate}" />
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBox Style="{StaticResource ValueBoxStyle}"  />
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

此我认为(没有测试它)会使。

<强>的结合问题

不过,你可以看到在Text属性结合缺失。现在有我能想到的几件事情可以帮你解决这个问题的结合,但他们既涉及滥用DataContext或创建一个子类ContentPresenter的帮助直通从外部控制属性内一个。两者都是真实的难看。

<强>结论

对于这样一个简单的基本模板你可能会更好过复制模板不是通过所有必要的篮球跳到acheive某种再利用。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top