3

I am trying to make a rule list for one of my usercontrols. List contains a custom type List<StringInputRule>. I am using DependancyProperty to databind.

I am trying to set the rules in xaml for the control as such:

<controlsDefault:DateEditWithStringInput>
    <controlsDefault:DateEditWithStringInput.Rules>
       <x:Array Type="controlsDefault:StringInputRule" xmlns:sys="clr-namespace:System;assembly=mscorlib">
            <controlsDefault:StringInputRule Key="T" Amount="1" Unit="Day"/>
            <controlsDefault:StringInputRule Key="W" Amount="1" Unit="Week"/>
            <controlsDefault:StringInputRule Key="M" Amount="1" Unit="Month"/>
       </x:Array>
     </controlsDefault:DateEditWithStringInput.Rules>
</controlsDefault:DateEditWithStringInput>

c# code for dependancy property for the control:

public partial class DateEditWithStringInput : UserControl
{
    public DateEditWithStringInput()
    {
        InitializeComponent();
        Rules = new List<StringInputRule>();
    }

    public IList<StringInputRule> Rules
    {
        get { return (IList<StringInputRule>)GetValue(RulesProperty); }
        set { SetValue(RulesProperty, value); }
    }

    public static readonly DependencyProperty RulesProperty =
        DependencyProperty.Register("Rules", typeof(IList<StringInputRule>), typeof(DateEditWithStringInput), new PropertyMetadata(new List<StringInputRule>()));
}

So the following approach does not pass any values, yet it compiles. I have read that generic types could be initialized within xaml from 2009 version with 4.0, yet I could have not located an example.

My question: How to define generic List in xaml?

Edit: As to this day there is no solid solution to the problem.

WorkAround (as pointed by Szabolcs Dézsi): how use List<T> within xaml?

Community
  • 1
  • 1
Karolis Kajenas
  • 1,523
  • 1
  • 15
  • 23

1 Answers1

0

It would look like this:

<controlsDefault:DateEditWithStringInput.Rules>
    <generic:List x:TypeArguments="controlsDefault:StringInputRule">
        <controlsDefault:StringInputRule Key="T" Amount="1" Unit="Day"/>
        <controlsDefault:StringInputRule Key="W" Amount="1" Unit="Week"/>
        <controlsDefault:StringInputRule Key="M" Amount="1" Unit="Month"/>
    </generic:List>
</controlsDefault:DateEditWithStringInput.Rules>

x:TypeArguments is a XAML 2009 feature. Unfortunately it's not supported in compiled XAML, so you won't be able to use it in your WPF application. Read more here, here and here.

Community
  • 1
  • 1
Szabolcs Dézsi
  • 8,743
  • 21
  • 29
  • 2
    Unfortunately, I cannot mark this as an answer since it just states the fact that you cannot do it, yet I have actually already solved it by adding additional non generic type which inherits from List and use child class as type. You can alter your answer accordingly and I will mark it as an answer. Have a good day! – Karolis Kajenas Feb 04 '16 at 07:43
  • 1
    Yes, I know about that, the question is a duplicate anyway: http://stackoverflow.com/a/7152297/4620101 although I feel this is a workaround and not an answer for your specific question: "How to define generic List in xaml?" – Szabolcs Dézsi Feb 04 '16 at 08:30
  • Initially I was looking for a more solid way to do it as you mentioned in the comment, that is why I written this questions. Apparently, you cannot do without that workaround :) Thanks for the effort! – Karolis Kajenas Feb 04 '16 at 08:52