我想在XAML中填充自定义字符串数组,但是收到错误。我子类组合框,并添加一个字符串[]我想,以填补自定义值:在XAML中填充自定义数组属性
public class MyComboBox : ComboBox
{
public string[] MyProperty { get { return (string[])GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } }
public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string[]), typeof(MyComboBox));
}
我的XAML如下:
<Window x:Class="Samples.CustomArrayProperty"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Samples"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="CustomArrayProperty" Height="300" Width="300">
<Grid>
<local:MyComboBox Height="20">
<local:MyComboBox.MyProperty>
<sys:String>Monday</sys:String>
<sys:String>Wednesday</sys:String>
<sys:String>Friday</sys:String>
</local:MyComboBox.MyProperty>
</local:MyComboBox>
</Grid>
</Window>
当我运行它,我得到的错误:“ '星期一'不是'MyProperty'属性的有效值。“
我在做什么错?
谢谢你,但我只是用一个ComboBox尝试简化问题。我继承了一个自定义控件,该控件具有代码中已填充的Enumerable属性,并且我希望尽可能使其对XAML友好,而无需更改对象的定义。 –
Michael
2011-05-11 17:04:29
好的,所以如果你改变你的依赖属性使用列表而不是String []会不会帮助你?当我这样做,我没有任何问题,设置属性“myProperty的=‘{StaticResource的StringList的}’” –
Liz
2011-05-11 17:41:06
是的,当我这样做,在一个示例项目它的工作原理,但它要求我改变我的业务对象的定义,我不当我这样做的时候,确定谁的脚趾会踩着(不幸的是,这个班似乎没有得到很好的测试 - 我将把这看作一个写更多的机会)。 – Michael 2011-05-11 18:18:23