Silverlight自定义控件继承。重复使用模板?

标签 silverlight inheritance custom-controls controltemplate reusability

我有以下情况:

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

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

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 并仅覆盖内容属性,保留模板的其余部分?

最佳答案

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

您当前遇到的问题是您正在尝试使用 ContentPresenterContent 属性。然而,这需要分配一个具体的控件实例,在本例中您使用的是 TextBox。您不能在 TextBox 中使用 TemplateBinding,因为它不是 ControlTemplate 的一部分。

即使没有 TemplateBinding 问题,您也只能用它创建一个控件,因为您无法“重复使用”TextBox 的同一实例> 在不止一处。这就是我们首先使用模板的原因。

全程模板

解决模板问题应该不难。真正棘手的事情是找到一种方法将专用控件的内部内容控件绑定(bind)到专用类的属性。当您无法使用 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>

我认为(未经测试)会渲染。

绑定(bind)问题

但是,正如您所看到的,缺少 Text 属性上的绑定(bind)。现在我能想到的一些事情可以帮助您解决这个绑定(bind)问题,但它们涉及滥用 DataContext 或创建 ContentPresenter 的子类来帮助您从外部控件到内部控件的传递属性。两个人都太丑了。

结论

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

关于Silverlight自定义控件继承。重复使用模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2250661/

相关文章:

mvvm - 绑定(bind)自定义文本框属性时出错

c# - 银光和 WCF : Max message size

c# - 如何在列表框wp7中实现搜索?

c# - Windows 手机 Silverlight 8.1 : Can't create app package

c++ - QT:私有(private)成员而不是继承?是什么原因?这是一个具体的概念吗?

delphi - 如何在编辑控件中偏移光标的位置?

silverlight - Silverlight 和 WCF 服务的通信异常

Java继承实例化

c++ - std::string 和 std::wstring 的前向声明

c# - 使用自定义绘制的控件滚动面板