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>();
4

2 回答 2

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.

于 2011-08-22T19:07:36.467 回答
-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>
...
于 2021-04-14T08:11:46.490 回答