3

我有以下情况:

[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 是任何输入框必不可少的面板。它现在很简单,但将在整个应用程序中重用,并将包含更复杂的行为和布局。

TextBox 作为输入是必须使用的,因此我制作了这个控件:

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 最常与 TextBox 一起使用,我想为此制作一个控件,它重用相同的模板,所以我不需要复制/粘贴模板,并且要让两者保持最新变化。

如何重用 ValueBoxWithLabelTemplate 并仅覆盖 content 属性,保留模板的其余部分?

4

1 回答 1

1

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

您当前遇到的问题是您正在尝试Content使用ContentPresenter. 但是,这需要分配控件的具体实例,在这种情况下,您正在使用TextBox. 您不能在 中使用TemplateBindingTextBox因为它不是ControlTemplate.

即使没有这个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来帮助将属性从外部控件传递到内部控件。两个都是真的丑

结论

对于这样一个简单的基本模板,您可能最好复制模板而不是跳过所有必要的箍来实现某种重用。

于 2010-02-12T14:48:59.757 回答