创建UserControl DependencyProperty,可以在下拉列表中选择其值(如组合框) [英] Create UserControl DependencyProperty of which value can be chosen in dropdown list (as combo box)
问题描述
我是WPF的入门者,现在我想制作一个WPF userControl库,其中包括一个评级栏userControl。创建评级栏的所有步骤均已完成,但是我想添加一个属性RatingValue:
I'm a starter at WPF, now i would like to make a WPF userControl library which include a Rating bar userControl. All the steps of creating the rating Bar has been done, however i would like to add a property RatingValue:
public static readonly DependencyProperty RatingValueProperty =
DependencyProperty.Register("RatingValue", typeof(int), typeof(RatingControl),
new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(RatingValueChanged)));
public int RatingValue
{
get { return (int)GetValue(RatingValueProperty); }
set
{
SetValue(RatingValueProperty, value);
}
}
private static void RatingValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
//... change the rating value
}
我的UserControl的用户可以修改0到5之间的值,如下所示属性窗口中的下拉列表(组合框)(某些用户控件的属性如可见性,Windows样式,背景...)
that the user of my UserControl can modify by a value from 0 to 5 that are shown in a dropdown list (combo box) in the Properties windows (as some exist properties of Usercontrols like Visibility, windows style, background ...)
我该怎么办?
非常感谢,
How can i do? Thank you very much in advance,
越南语
推荐答案
- 创建一个从TypeConverter派生的类。
- 覆盖GetStandardValues和GetStandardValuesSupported(以及可选的GetStandardValuesExclusive)。
- 从GetStandardValues ,返回一个包含要显示在组合框中的值的集合。
- 将TypeConverterAttribute应用于RatingValue属性,指定类型转换器的类型。
- Create a class derived from TypeConverter.
- Override GetStandardValues and GetStandardValuesSupported (and optionally GetStandardValuesExclusive).
- From GetStandardValues, return a collection containing the values you want to appear in the combo box.
- Apply TypeConverterAttribute to the RatingValue property, specifying the type of your type converter.
或者,根据RatingValue的语义,您可以考虑将其设为枚举。因为值是数字,所以感觉有点奇怪-但这将具有在类型级别上约束值的优势,并且它会自动为您提供一个组合框,而无需您实现类型转换器。
Alternatively, depending on the semantics of RatingValue, you might consider making it an enum. This feels a bit weird because the values are numeric -- but it would have the advantage of constraining the values at a type level, and it would automatically give you a combo box with no need for you to implement a type converter.
这篇关于创建UserControl DependencyProperty,可以在下拉列表中选择其值(如组合框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!