8

How I can set value of string[] property in xaml?

I hava control with next property: string[] PropName

I want to set value of this property in next way:

<ns:SomeControl PropName="Val1,Val2" />

4 Answers 4

11

You can use the <x:Array> markup extension, but its syntax is quite verbose.

Another option is to create your own TypeConverter that can convert from the comma-separated list to an array:

class ArrayTypeConverter : TypeConverter
{
    public override object ConvertFrom(
        ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        string list = value as string;
        if (list != null)
            return list.Split(',');

        return base.ConvertFrom(context, culture, value);
    }

    public override bool CanConvertFrom(
        ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string))
            return true;

        return base.CanConvertFrom(context, sourceType);
    }
}

If the type you were converting to was your type, you could then apply the [TypeConverter] attribute to that type. But since you want to convert to string[], you can't do that. So you have to apply that attribute to all properties where you want to use this converter:

[TypeConverter(typeof(ArrayTypeConverter))]
public string[] PropName { get; set; }
Sign up to request clarification or add additional context in comments.

Comments

3
     <ns:SomeControl>
        <SomeControl.PropName>
            <x:Array Type="sys:String">
                <sys:String>Val1</sys:String>
                <sys:String>Val2</sys:String>
            </x:Array> 
        </SomeControl.PropName>
    </ns:SomeControl>

1 Comment

+1. I just realized that you proposed this answer in sll's comments way before I wrote mine.
2

The idea is to define custom values as Array in resources of a control/window and then just use Binding to a static resource:

<!-- or Window.Resources -->
<UserControl.Resources>
    <x:Array x:Key="CustomValues" 
             Type="sys:String"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"> 
        <sys:String>Val1</sys:String> 
        <sys:String>Val2</sys:String> 
    </x:Array> 
</UserControl.Resources>

<!-- Then just bind -->
<ns:SomeControl PropName="{Binding Source={StaticResource CustomValues}}" />

12 Comments

Why binding to StaticResource? Why resource at all?
+1, this is the way to go for values known at compile time. Now if he needs dynamic values (runtime values, bound, etc) he's going to need an I(Multi)ValueConverter that returns an array.
@Snowbear : this is what I know for now, I would appreciate any more advanced solution, why you've deleted your answer and do not provided an answer? Really I've no other idea how to do it so would +1 if you show an other way, thanks in advance
@sll, I deleted my answer because it is formatted weirdly due to some unknown reason, trying to figure that out right now
I know all values at compile time, so do not need to bind any dynamic values.
|
2

sll's answer is great, but you can avoid the resource if you want and write the value directly into the control:

<ns:SomeControl>
    <ns:SomeControl.PropName>
        <x:Array Type="sys:String" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                 xmlns:sys="clr-namespace:System;assembly=mscorlib">  
            <sys:String>Val1</sys:String>  
            <sys:String>Val2</sys:String>  
        </x:Array>  
    </ns:SomeControl.PropName>
</ns:SomeControl> 

In addition, you can move the xmlns: declarations into the head element (Window, UserControl, etc.), so you don't clutter your control properties with it.

PS: If you are the one developing SomeControl, I'd use svick's approach and provide a TypeConverter.

1 Comment

The x:Key is redundant when it's not a resource.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.