开发者

Populating custom array property in XAML

I am trying to populate a custom string array in XAML, but am receiving an error. I subclassed a ComboBox and added a string [] that I want to fill with custom values:

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));
}

My XAML is as follows:

<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>

When I run this, I get the error: "'Monday' is not a valid value for property 'MyProperty'.".

What am I doing wrong?


You can create arrays in XAML using x:Array, you still need to use it as a resource like Liz's answer.

<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}" />


Is there any other reason you are subclassing the ComboBox beyond what you are doing here?

Because if the idea is to just provide a list of strings to the combobox then take a look at "Add collection or array to wpf resource dictionary"

As much as possible it would be better to let the combobox take care of itself - by using the ItemsSource property. So what we are doing in the linked example is providing a 'resource' containing your list of strings, and then passing this resource to the ItemsSource as follows:

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

Define a type like this:

public class StringList : List<string> { }

And then create a static resource

<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>

I hope that helps.

Edit: You could also change your DependencyProperty to use StringList instead of String[], then your dependency property will work as well.

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

And then the 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;
    }

Then this would effectively do the same thing as binding directly to the ItemsSource. But the main thing if I understand you correctly, is just to get the Dependency property to work.


And, an extra example with 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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜