Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populating custom array property in XAML

Tags:

arrays

wpf

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?

like image 590
Michael Avatar asked May 11 '11 15:05

Michael


2 Answers

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}" />
like image 67
Kris Avatar answered Oct 06 '22 09:10

Kris


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.

like image 41
Liz Avatar answered Oct 06 '22 09:10

Liz