2015-01-04 94 views
0

我将使用一组控件,每个控件都由一个标签和一个文本框组成。宣布自定义控件是否是一个好主意,以便将标签的标题和文本框的输入区域封装为一个整体?声明自定义控件

我可以在C#中继承和创建一些东西,但这里的重点是要在XAML中变得更好,所以通过标记声明这样的东西(不知道它是否称为资源,模板,样式或其他)是优选的(如果还没有必要的话)。

目前,我采用了以下方法,但我不确定未来是否会为自己创造更多的头痛。

<StackPanel Grid.Row="0" Grid.Column="0"> 
    <Label Content="Boom" Style="{StaticResource DefaultInputStyle}" /> 
    <DatePicker Style="{StaticResource DefaultInputStyle}" /> 
</StackPanel> 

这个想法最好能够使用类似的东西。

目前,我采用了以下方法,但我不确定未来是否会为自己创造更多的头痛。

<MyCoolControl Grid.Row="0" Grid.Column="0" 
       Content="Boom" 
       Style="{StaticResource DefaultInputStyle}" /> 
+0

我有没有这样的MVC的,但会你也可以在wpf中使用。 – Saravanan 2015-01-04 11:05:12

+0

MVC创建一个HTML标记并在客户端动态设置它。 WPF能够编译时间,所以我不确定这些是如何翻译的。你能否给出一个关于如何声明如此复杂控制的例子?或建议在评论关键词集中? – 2015-01-04 11:10:40

+2

注意:我想你是在讨论一个'UserControl'而不是'custom control'。对于区别请参阅:http://wpftutorial.net/CustomVsUserControl.html – 2015-01-04 11:58:52

回答

0

我不确定您是否可以为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}" /> 
+0

至于你的问题*什么是DefaultInputStyle的TargetType?* - 我骗了。我把* TargetType * * UserControl *,嘿嘿。只是为了这个例子。至于你的回复 - 这是否意味着我**不能从XAML那里做到这一点? ID。我**必须**继承和玩C#和代码后面? (我想掌握XAML,是这个想法,C#我已经被覆盖了,非常好) – 2015-01-04 12:16:06

+0

如果你编写自定义控件,你必须一起玩C#(VB)和XAML。在你的情况下,使用你的自定义控件要容易得多。 – user2250152 2015-01-04 12:26:56