我不确定您是否可以为Label和DatePicker设置DefaultInputStyle。什么是DefaultInputStyle的TargetType?如果您将在多个应用程序中使用此自定义控件,建议使用自定义控件。如果你想创建自定义控件,你需要继承控件,创建一些依赖属性,重写DefaultStyleKeyProperty。
public class MyCoolControl : Control
{
public Style LabeStyle
{
get { return (Style)GetValue(LabeStyleProperty); }
set { SetValue(LabeStyleProperty, value); }
}
public static readonly DependencyProperty LabeStyleProperty =
DependencyProperty.Register(
"LabeStyle", typeof(Style), typeof(MyCoolControl));
public Style DatePickerStyle
{
get { return (Style)GetValue(DatePickerStyleProperty); }
set { SetValue(DatePickerStyleProperty, value); }
}
public static readonly DependencyProperty DatePickerStyleProperty =
DependencyProperty.Register(
"DatePickerStyle", typeof(Style), typeof(MyCoolControl));
public object LabelContent
{
get { return (object)GetValue(LabelContentProperty); }
set { SetValue(LabelContentProperty, value); }
}
public static readonly DependencyProperty LabelContentProperty =
DependencyProperty.Register(
"LabelContent", typeof(object),
typeof(MyCoolControl), new PropertyMetadata(null));
static MyCoolControl()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(MyCoolControl),
new FrameworkPropertyMetadata(typeof(MyCoolControl)));
}
}
定义隐含风格MyCoolControl在主题/ Generic.xaml:
<Style TargetType="local:MyCoolControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<Label Content="{TemplateBinding LabelContent}" Style="{TemplateBinding LabeStyle}" />
<DatePicker Style="{TemplateBinding DatePickerStyle}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
然后你可以使用自定义控制:
<local:MyCoolControl Grid.Row="0" Grid.Column="0"
LabelContent="Boom" DatePickerStyle="{StaticResource DefaultInputDatePickerStyle}"
LabelStyle="{StaticResource DefaultInputLabelStyle}" />
我有没有这样的MVC的,但会你也可以在wpf中使用。 – Saravanan 2015-01-04 11:05:12
MVC创建一个HTML标记并在客户端动态设置它。 WPF能够编译时间,所以我不确定这些是如何翻译的。你能否给出一个关于如何声明如此复杂控制的例子?或建议在评论关键词集中? – 2015-01-04 11:10:40
注意:我想你是在讨论一个'UserControl'而不是'custom control'。对于区别请参阅:http://wpftutorial.net/CustomVsUserControl.html – 2015-01-04 11:58:52