开发者

How to bind a TreeView Hierarchically by using enum values in WPF

Ok, so I am not sure this is even possible but I will ask the question here and hope to get an answer to it.

Suppose I have a list of a class, say Media items defined as follows

Enum MediaItemType{
  Book,
  CD,
  VideoGame
}

public M开发者_高级运维ediaItem{
  public string Name { get; set; }
  public MediaItemType { get; set; }
}

Now suppose I want to bind a list of MediaItems to a TreeView such that each MediaItem ends upp in a separate subtree depending on the media item type. Is that possible to do and if so, how do I do that?

In my Xaml-code I assume that I have defined the list as a property named MediaItems in the context.

<Grid>
   <TreeView ItemsSource="{Binding Path=MediaItems}">
</Grid>

The tree view should be something like this

Book
  - In to the wild
  - Code Complete
CD
  - Foo Fighters
  - Bach
DVD
  - X-men
  - Casino Royale


Don't see any problem. In model you have MediaItem, on model view you have to have MediaItemView type, something like this:

public class MediaItemView
{
    public MediaItemType { get; set; }
    public List<MediaItem> medialist;
}

Define bindings on that class, and define a Converter which will convert enum value to its string presentation.


I don't know about about a tree view, but you could achieve something similar to what you describe using a GroupDescription grouping on the MediaItemType property with a ListBox or ListView as described here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜