Hi. I'm trying to display a ListBox
inside of a GridViewColumn
that needs to bind to a List of enums (List<ResourceType> Cost
). The GridViewColumn
's ListView
is already bound to a collection of objects and I'm not really sure the best way to go about displaying the ListBox
. Any suggestions?
views:
20answers:
1
A:
You can bind the ListBox
to a list of enum values. An easy way to do that is to use the markup extension I posted here.
Then, you need to bind the SelectedItem
of the ListBox
to the property displayed in the GridViewColumn
.
You should end up with something like that :
<GridViewColumn Header="Resource type">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ListBox ItemsSource="{local:EnumValues local:ResourceType}"
SelectedItem="{Binding SelectedResourceType}">
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
EDIT: I think I misread your question... If I understand correctly, each object displayed in the ListView
has a Cost
property of type List<ResourceType>
, right ? (btw, the fact that ResourceType
is an enum doesn't matter here). So you just need to bind the ListBox
to the Cost
property :
<GridViewColumn Header="Resource type">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding Cost}">
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
Thomas Levesque
2010-07-17 01:37:42
You beat me to it. You are correct. The first interpretation was a little off, but your edit hits the nail on the head. Curious though; why does it not matter that the `ResourceType` is an `enum`? Is it because WPF automatically calls the `ToString()` method on the selected `enum` and displays it? Thanks for the fast answer by the way.
SirBeastalot
2010-07-17 19:14:00
Yes, that's it. Unless a DataTemplate is defined for the type, ToString is called to get a representation of the object
Thomas Levesque
2010-07-17 21:05:37