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