2011-05-11 71 views
2

我想在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'屬性的有效值。「

我在做什麼錯?

回答

1

是否有任何其他的原因,你是否繼承了你在這裏做的ComboBox的子類?

因爲如果這個想法是隻提供字符串組合框列表,然後看看"Add collection or array to wpf resource dictionary"

儘可能倒不如讓組合框照顧自己 - 通過使用的ItemsSource屬性。因此,我們在鏈接的例子在做什麼,是提供包含您的字符串列表中的「資源」,然後通過這個資源的的ItemsSource如下:

<ComboBox ItemsSource="{StaticResource stringList}" /> 

定義類型是這樣的:

public class StringList : List<string> { } 

然後創建一個靜態資源

<Window.Resources> 
    <local:StringList x:Key="stringList"> 
     <sys:String>Monday</sys:String> 
     <sys:String>Wednesday</sys:String> 
     <sys:String>Friday</sys:String> 
    </local:StringList > 
</Window.Resources> 

我希望幫助。

編輯:你也可以改變你的DependencyProperty使用StringList而不是String [],那麼你的依賴屬性也可以。

<local:MyComboBox MyProperty="{StaticResource stringList}" Height="23" /> 

然後是的DependencyProperty:

public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty",typeof(StringList),typeof(MyComboBox),new FrameworkPropertyMetadata(null, listChangedCallBack)); 

    static void listChangedCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args) 
    { 
     ComboBox combo = (ComboBox)property; 
     combo.ItemsSource= (IEnumerable)args.NewValue; 
    } 

那麼這將有效地做同樣的事情,直接綁定到的ItemsSource。但是,如果我正確理解你的主要事情,只是爲了讓依賴屬性起作用。

+0

謝謝你,但我只是用一個ComboBox嘗試簡化問題。我繼承了一個自定義控件,該控件具有代碼中已填充的Enumerable 屬性,並且我希望儘可能使其對XAML友好,而無需更改對象的定義。 – Michael 2011-05-11 17:04:29

+0

好的,所以如果你改變你的依賴屬性使用列表而不是String []會不會幫助你?當我這樣做,我沒有任何問題,設置屬性「myProperty的=‘{StaticResource的StringList的}’」 – Liz 2011-05-11 17:41:06

+0

是的,當我這樣做,在一個示例項目它的工作原理,但它要求我改變我的業務對象的定義,我不當我這樣做的時候,確定誰的腳趾會踩着(不幸的是,這個班似乎沒有得到很好的測試 - 我將把這看作一個寫更多的機會)。 – Michael 2011-05-11 18:18:23

8

您可以使用x:Array在XAML中創建數組,您仍然需要使用它作爲資源,如Liz的答案。

<Window.Resources> 
    <x:Array Type="sys:String" x:Key="days"> 
     <sys:String>Monday</sys:String> 
     <sys:String>Wednesday</sys:String> 
     <sys:String>Friday</sys:String> 
    </x:Array> 
</Window.Resources> 


<local:MyComboBox Height="23" MyProperty="{StaticResource days}" /> 
+0

所以,如果我有一個UIElement屬性是一個數組Type(string []例如),我必須直接設置數組的值?我希望有一種方法來在線插入數組屬性。 – Michael 2011-05-11 19:01:51

+0

你可以嘗試把x:array內聯,對我來說編譯和工作,但經常在編輯過程中出現錯誤,我不確定它是否受支持,並且行爲在v3.5和4.0之間確實發生了變化。如果你需要經常這樣做,你可以編寫一個標記擴展來分割一個分隔的字符串,這可能是一行XAML。 – Kris 2011-05-11 19:12:53

+0

你的意思是 local:MyComboBox.MyProperty> ... values here ...?這並不適合我,因爲它認爲我試圖將一個字符串[]作爲單個元素添加到字符串[]中。 – Michael 2011-05-11 19:20:18

0

而且,額外的例子有enum

namespace MyNamespace 
{ 
    public enum OrganizationType 
    { 
     Office365, 
     OnPremisses 
    } 
} 

xmlns:local="clr-namespace:MyNamespace" 

<x:Array Type="local:OrganizationType" x:Key="OrganizationTypeArray"> 
    <local:OrganizationType>Office365</local:OrganizationType> 
    <local:OrganizationType>OnPremisses</local:OrganizationType> 
</x:Array>