简体   繁体   中英

WPF VS Collection editor tutorial?

I have a usercontrol with a Collection property. What I want to achieve is to be able to add/modify/remove items of some data types of that collection via VS designer (Property window/Collection editor).

I have a simple class:

public class Quantity
{
    public string Name { get; set; }
    public Type DataType { get; set; }
}

In my UserControl I have:

private ObservableCollection<Quantity> _quantities = new ObservableCollection<Quantity>();
public ObservableCollection<Quantity> Quantities
{
    get { return _quantities; }
}

And the thing is that I am able to change the Name property via that VS Collection editor but I am unable to change DataType property that way.

通过VS集合编辑器修改集合

So what do I have to do to make it work?

I don't believe this can be achieved through the properties editor. You can however produce the result in XAML. Here is what it would look like using your example:

<my:UserControl1>
    <my:UserControl1.Quantites>
        <my:Quantity Name="Hello World" DataType="{x:Type sys:Boolean}"/>
        <my:Quantity Name="This is a double" DataType="{x:Type sys:Double}"/>
    </my:UserControl1.Quantites>
</my:UserControl1>

If you need access to system types (like I used in my example) you can include the following xmlns:

xmlns:sys="clr-namespace:System;assembly=mscorlib"

Hope it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM