我想在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