WPF用户控件vs自定义控件

时间:2021-08-14 15:59:22

I am trying to creating a combobox with checkboxes on each line to allow multiple selecting. Would this be better as a User Control or Custom Control?

我正在尝试创建一个包含每一行复选框的组合框,以允许多个选择。作为用户控件还是自定义控件,这更好吗?

I haven't created a control before so just looking for a little advice on what direction I need to head.

我之前还没有创建控件,所以只是想找一些建议,告诉我该往哪个方向走。

Thanks.

谢谢。

2 个解决方案

#1


13  

I would say use a datatemplate.

我想说使用一个datatemplate。

Like this: Looking for a WPF ComboBox with checkboxes

像这样:寻找一个带有复选框的WPF组合框

It's a lot more simple than trying to create your own control. :)

这比尝试创建自己的控件要简单得多。:)

#2


67  

UserControl (Composition)

用户控件(作文)

  • Composes multiple existing controls into a reusable "group"
  • 将多个现有控件组合成可重用的“组”
  • Consists of a XAML and a code behind file
  • 由XAML和文件后面的代码组成
  • Cannot be styled/templated
  • 不能风格/模板化
  • Derives from UserControl
  • 来自用户控件

CustomControl (Extending an existing control)

CustomControl(扩展现有控件)

  • Extends an existing control with additional features
  • 使用附加特性扩展现有控件
  • Consists of a code file and a default style in Themes/Generic.xaml
  • 由主题/ general .xaml中的代码文件和默认样式组成
  • Can be styled/templated
  • 可以设计/模板吗
  • The best approach to build a control library
  • 构建控制库的最佳方法

In your case, I think UserControl would be better; here's an example for you:

在你的案例中,我认为UserControl会更好;举个例子:

<CheckBox Content="Property" IsChecked="{Binding Path=SomeProperty}" />
<ComboBox IsEnabled="{Binding Path=Enabled}" />

#1


13  

I would say use a datatemplate.

我想说使用一个datatemplate。

Like this: Looking for a WPF ComboBox with checkboxes

像这样:寻找一个带有复选框的WPF组合框

It's a lot more simple than trying to create your own control. :)

这比尝试创建自己的控件要简单得多。:)

#2


67  

UserControl (Composition)

用户控件(作文)

  • Composes multiple existing controls into a reusable "group"
  • 将多个现有控件组合成可重用的“组”
  • Consists of a XAML and a code behind file
  • 由XAML和文件后面的代码组成
  • Cannot be styled/templated
  • 不能风格/模板化
  • Derives from UserControl
  • 来自用户控件

CustomControl (Extending an existing control)

CustomControl(扩展现有控件)

  • Extends an existing control with additional features
  • 使用附加特性扩展现有控件
  • Consists of a code file and a default style in Themes/Generic.xaml
  • 由主题/ general .xaml中的代码文件和默认样式组成
  • Can be styled/templated
  • 可以设计/模板吗
  • The best approach to build a control library
  • 构建控制库的最佳方法

In your case, I think UserControl would be better; here's an example for you:

在你的案例中,我认为UserControl会更好;举个例子:

<CheckBox Content="Property" IsChecked="{Binding Path=SomeProperty}" />
<ComboBox IsEnabled="{Binding Path=Enabled}" />