3

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.

Modifying collection via VS collection editor

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

Nikolas Jíša
  • 699
  • 1
  • 11
  • 29

1 Answers1

1

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 :)

deloreyk
  • 1,248
  • 15
  • 24
  • As a suggestion, I would highly recommend using XAML over the properties editor as much as you can. Once you get used to writing things in XAML, the properties editor will become useless to you. – deloreyk Mar 01 '13 at 05:50
  • Thanks for the answer, yes that is a possibility, but unfortunately I have to make it work via properties window somehow. I got this idea to change the data type of my DataType property to string (and change the setter so it accepts only strings of a type) and then have another property which would be like "{get { return GetType(DataType); } }" however I quite don't like this solution, but perhaps there is not a simplier way. – Nikolas Jíša Mar 01 '13 at 10:50