3

我有以下情形:Silverlight自定义控件继承。重用模板?

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

回答

1

它是一种有趣的方法。我没有尝试过,但看起来这种方法可能会起作用。

您目前拥有的问题是您正在尝试使用ContentPresenterContent属性。然而,这需要分配一个具体的控件实例,在这种情况下,您正在使用TextBox。您不能在TextBox中使用TemplateBinding,因为它不是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以帮助从外部控制到内部控制的传递属性。两者都是真正的丑陋。

结论

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

+0

感谢您的反馈意见。希望本周末不会得到时间,但我会尝试它的第一件事星期一,并报告回来:-) – 2010-02-12 20:35:02

+0

再次安东尼。我尝试了解你的解决方案,但没有运气。 问题是,即时通讯无法访问控件模板中的“部件”,因此上面的以下行将不起作用: ((DoubleClickButton)GetTemplateChild(GoToEditStateButtonPart))。DoubleClick + =(sender,args)= > IsInEditMode = true; 有什么想法? – 2010-02-15 17:04:33

+0

你在这个问题上有很多好的指针。由于我们目前项目的时间压力。我没有时间深入挖掘。我可能会稍后发布。感谢您的输入。 – 2010-02-21 10:13:01