声明自定义控件 [英] Declaring custom controls

查看:79
本文介绍了声明自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用一堆控件,每个控件都包含一个标签和一个文本框。声明一个自定义控件,以便将标签的标题和文本框的输入区域都封装为一个,是一个好主意吗?

I'm going to use a bunch of controls that each consist of a label and a text box. Is it a good idea to declare a custom control so that in encapsulates both the caption of the label and the input area of the text box as one?

我可能可以继承并在C#中创建一些东西,但是这里的重点是要更好地使用XAML,因此最好使用通过标记声明这样的东西(不确定是否称为资源,模板,样式或其他任何东西)的方法(如果尚不需要的话) )。

I can probably inherit and create something in C# but the point here is to get better at XAML so the approach of declaring such a thingy (not sure if it's called resource, template, style or anything else) by a markup is to be preferred (if not yet necessary).

此刻,我采用了以下方法,但不确定将来是否不会给自己造成更多头痛。

At the moment, I went with the following approach but I'm not sure if I'm not creating more headache for myself in the future.

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

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

The idea would be optimally to be able to use something like so.

此刻,我采用了以下方法,但不确定将来是否不会让自己感到头疼。

At the moment, I went with the following approach but I'm not sure if I'm not creating more headache for myself in the future.

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


推荐答案

我不确定是否可以同时设置DefaultInputStyle标签和日期选择器。什么是DefaultInputStyle的TargetType?如果要在多个应用程序中使用此自定义控件,建议使用自定义控件。如果要创建自定义控件,则需要从控件继承,创建一些依赖项属性,重写DefaultStyleKeyProperty。

I'm not sure if you can set DefaultInputStyle both for Label and for DatePicker. What is TargetType of DefaultInputStyle? Custom control is recommended in case that you will use this custom controle in more than one application. If you want to create custom control you need to inherit from control, create some dependency properties, override 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)));
  }
}

在Themes / Generic.xaml中定义MyCoolControl的隐式样式:

Define implicit style for MyCoolControl in Themes/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>

然后您可以使用自定义控件:

Then you can use your custom control:

<local:MyCoolControl Grid.Row="0" Grid.Column="0"
           LabelContent="Boom" DatePickerStyle="{StaticResource DefaultInputDatePickerStyle}"
           LabelStyle="{StaticResource DefaultInputLabelStyle}" />

这篇关于声明自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆