[Unity] Observablecollection problem.
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.
and this is how combobox looks like,

the actual code takes items from a enumeration like this
and result is exactly same, correct amount of items but names are "namespace.class"
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.
Code: Select all
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"}
};
}
}
}
}

the actual code takes items from a enumeration like this
Code: Select all
foreach (Enumerations.ArenaStyle arenaStyle in Enum.GetValues(typeof(Enumerations.ArenaStyle)))
{
ArenaStyles.Add(new ArenaStyle(arenaStyle));
}
-
-
sfernandez
Site Admin
- Posts: 2065
- Joined:
Re: [Unity] Observablecollection problem.
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
- ItemTemplate
Using a DataTemplate allows you to show more complex representations of each item, and it is probably what you will end using.
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
Code: Select all
<ComboBox ItemsSource="{Binding ArenaStyles}" DisplayMemberPath="style"/>
Code: Select all
<ComboBox ItemsSource="{Binding ArenaStyles}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding style}"/>
</DataTemplate>
</ComboBox>
Re: [Unity] Observablecollection problem.
Groovy!
Thanks for your reply.
It looks like I overlooked datatemplate part from documentation but now it works, thanks for your support.
Thanks for your reply.
It looks like I overlooked datatemplate part from documentation but now it works, thanks for your support.
-
-
sfernandez
Site Admin
- Posts: 2065
- Joined:
Re: [Unity] Observablecollection problem.
Thanks, I marked the post as solved.
Re: [Unity] Observablecollection problem.
By the way, you no longer need to inherit from BaseComponent in your view models. This is fine:
Code: Select all
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 0 guests