5

So I am pretty sure that up in the definition part I need to include something along the lines of:

xmlns:s="clr-namespace:System.Collections.Generic;assembly=?????" 

but I just do not know what to put in place of the ???'s.

What I want to do with the code is this:

<UserControl.DataContext>
    <ObjectDataProvider 
          MethodName="CreateNodes"
          ObjectType="{x:Type local:TreeViewModel}" >
        <ObjectDataProvider.MethodParameters>
            <s:List<T>>
                  {Binding Nodes}
            </s:List<T>>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</UserControl.DataContext>

So that when I make the objectDataProvider call, I can pass the list in to the method that it is calling (CreateNodes)...

How do I go about doing this?

thanks!

Edit - could be a fix?

I just put this in the method, instead of passing in the list, it is just an app variable...I dont know if app variables are bad though

  List<TNode> existingNodes;

  if (Application.Current.Properties.Contains("ExistingNodes")) existingNodes = Application.Current.Properties["ExistingNodes"] as List<TNode>;
  else existingNodes = new List<TNode>();
Toadums
  • 2,772
  • 8
  • 44
  • 67
  • 1
    I don't believe XAML supports generics at this point. – Dan Bryant Aug 22 '11 at 19:00
  • The relevant assembly is `mscorlib.dll`, but I'm leaving this as a comment because I have never done that in XAML and I don't want to leave a partial answer. I do know that XAML does not support generics. – Ed S. Aug 22 '11 at 19:00
  • so I cant do what I am trying to do? :( or will I have to use application variables to pass the list? – Toadums Aug 22 '11 at 19:02
  • Generics are not supported in XAML. But you can create a class that inherits from the List with your proper generic type, and use that in XAML. – dowhilefor Aug 22 '11 at 19:06
  • @dowhilefor is it better to create the class, or do what I did in my edit above with application variables? – Toadums Aug 22 '11 at 19:17

2 Answers2

6

The assembly part of the XML namespace declaration would be mscorlib.

But anyway, XAML doesn't support generics (*), so you can't do it. Instead, you could create a class that inherits List<T> and use it in XAML:

class ListOfFoo : List<Foo>
{
}

(1) Actually generics are supported in XAML 2009, but most of XAML 2009 is not supported in compiled XAML. See this question for more information.

Community
  • 1
  • 1
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • I made an edit above. Could you tell me if it is better to do it the way you suggested (making a class), or the way I did it with application variables? :) – Toadums Aug 22 '11 at 19:12
  • @Toadums, it's probably not the cleanest way, but it should work, yes – Thomas Levesque Aug 22 '11 at 19:57
  • Even after writing such a class, it [cannot be used as the root element in a XAML file](https://msdn.microsoft.com/en-us/library/ms747086(v=vs.110).aspx). (Just spent an afternoon trying to understand why this wasn't working.) – Zev Spitz Nov 20 '16 at 17:01
-2

In my case, this is ok. If you define List property like this:

    internal static readonly BindableProperty TestListProperty = BindableProperty.Create("TestList", typeof(List<string>), typeof(View), null, propertyChanged: (bindable, oldValue, newValue) =>
    {
        var view = (View)bindable;
        List<string> v = (List<string>)newValue;
        view.testList = v;
    },
    defaultValueCreator: (bindable) =>
    {
        var view = (View)bindable;
        return view.testList;
    });
    private List<string> testList;
    public List<string> TestList
    {
        get
        {
            return (List<string>)GetValue(TestListProperty);
        }
        set
        {
            SetValue(TestListProperty, value);
        }
    }

Then you use it in XAML:

xmlns:s="clr-namespace:System.Collections.Generic;assembly=netstandard"
...
<View>
    <View.TestList>
        <s:List x:TypeArguments="x:String" >
           <x:String>abc</x:String>
           <x:String>123</x:String>
           <x:String>456</x:String>
        </s:List>
    </View.TestList>
</View>
...