私は次のシナリオを持っています:
[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を再利用し、テンプレートの残りの部分を保持したままコンテンツプロパティのみをオーバーライドするにはどうすればよいですか?