Populating a custom array property in XAML

I am trying to populate my own array of strings in XAML, but I am getting an error. I subclassed the ComboBox and added the [] line that I want to populate 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 looks like this:

<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 the "MyProperty" property. ".

What am I doing wrong?

+3
source share
3 answers

You can create arrays in XAML using x: Array , you still need to use it as a resource, for example, 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}" />
+8
source

, ComboBox , ?

, , combobox, wpf

, , combobox - 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. , , Dependency .

+1

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
source

All Articles