我有以下情形: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