Etana
Topic Author
Posts: 17
Joined: 21 Aug 2016, 13:14

[Unity] Observablecollection problem.

12 Sep 2016, 19:28

Hi,
I'm new here and actually new in whole WPF world but I think I'm getting hang of it quite nicely but now I've faced small problem which I just can't solve. No idea if it's actually a unity's problem or not but AFAIK, my code and xaml should be correct..

I have this in my code, it's more complicated in my actual file but I simplified it as much as possible.
    public class CokEditorDataModel : BaseComponent
    {
        public class ArenaStyle
        {
            public string style { get; set; }
        }
        public class ArenaData
        {
            public ObservableCollection<ArenaStyle> arenastyle { get; private set; }
            public ArenaData()
            {
                arenastyle = new ObservableCollection<ArenaStyle>()
                {
                    new ArenaStyle() { style = "test"},
                    new ArenaStyle(){ style = "test2"},
                    new ArenaStyle(){ style = "test3"}
                };
            }
        }
    }
}
and this is how combobox looks like,

Image

the actual code takes items from a enumeration like this

foreach (Enumerations.ArenaStyle arenaStyle in Enum.GetValues(typeof(Enumerations.ArenaStyle)))
{
                    ArenaStyles.Add(new ArenaStyle(arenaStyle));
}
and result is exactly same, correct amount of items but names are "namespace.class"
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: [Unity] Observablecollection problem.

13 Sep 2016, 00:27

Welcome Etana!

If nothing else is specified when an item is added to an ItemsControl, its ToString() method is used, which by default prints the type full name.

You can customize what is shown for each item in two ways:

- DisplayMemberPath
<ComboBox ItemsSource="{Binding ArenaStyles}" DisplayMemberPath="style"/> 
- ItemTemplate
<ComboBox ItemsSource="{Binding ArenaStyles}">
  <ComboBox.ItemTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding style}"/>
    </DataTemplate>
</ComboBox> 
Using a DataTemplate allows you to show more complex representations of each item, and it is probably what you will end using.
 
Etana
Topic Author
Posts: 17
Joined: 21 Aug 2016, 13:14

Re: [Unity] Observablecollection problem.

13 Sep 2016, 10:32

Groovy!
Thanks for your reply.
It looks like I overlooked datatemplate part from documentation but now it works, thanks for your support.
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: [Unity] Observablecollection problem.

13 Sep 2016, 19:43

Thanks, I marked the post as solved.
 
User avatar
jsantos
Site Admin
Posts: 2900
Joined: 20 Jan 2012, 17:18
Contact:

Re: [Unity] Observablecollection problem.

13 Sep 2016, 22:49

By the way, you no longer need to inherit from BaseComponent in your view models. This is fine:
public class CokEditorDataModel
{
    public class ArenaStyle
    {
        public string style { get; set; }
    }
    public class ArenaData
    {
        public ObservableCollection<ArenaStyle> arenastyle { get; private set; }
        public ArenaData()
        {
            arenastyle = new ObservableCollection<ArenaStyle>()
            {
                new ArenaStyle() { style = "test" },
                new ArenaStyle() { style = "test2" },
                new ArenaStyle() { style = "test3" }
            };
        }
    }
}

Who is online

Users browsing this forum: No registered users and 1 guest