我有以下情形: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並僅覆蓋保留模板其餘部分的內容屬性?
感謝您的反饋意見。希望本週末不會得到時間,但我會嘗試它的第一件事星期一,並報告回來:-) – 2010-02-12 20:35:02
再次安東尼。我嘗試瞭解你的解決方案,但沒有運氣。 問題是,即時通訊無法訪問控件模板中的「部件」,因此上面的以下行將不起作用: ((DoubleClickButton)GetTemplateChild(GoToEditStateButtonPart))。DoubleClick + =(sender,args)= > IsInEditMode = true; 有什麼想法? – 2010-02-15 17:04:33
你在這個問題上有很多好的指針。由於我們目前項目的時間壓力。我沒有時間深入挖掘。我可能會稍後發佈。感謝您的輸入。 – 2010-02-21 10:13:01