我一直在使用WPF UI控件。
我已经定义了一个依赖关系属性,它是一个字符串列表。
这将按预期与视图模型上的list属性绑定。
我想做的是可以选择在XAML中定义列表,而不是绑定到视图模型上的列表。
<local:MyControl MyList = "one,two,three">
我的控件上的MyList属性。
public static readonly DependencyProperty MyListProperty =
DependencyProperty.Register("MyList", typeof(List<string>), typeof(MyControl));
为了支持从包含逗号分隔元素的字符串中初始化列表,例如
MyList="one, two, three"
您将注册自定义TypeConverter
。
请注意,下面的代码使用TypeConverter
作为属性类型,这为可分配给该属性的类型提供了更大的灵活性,从而简化了TypeConverter的实现(返回IList<string>
)。
string[]