如何使用 WPF 中的枚举值分层绑定 TreeView
好吧,所以我不确定这是否可能,但我会在这里提出问题并希望得到答案。
假设我有一个类列表,假设媒体项定义如下。
Enum MediaItemType{
Book,
CD,
VideoGame
}
public MediaItem{
public string Name { get; set; }
public MediaItemType { get; set; }
}
现在假设我想将 MediaItems 列表绑定到 TreeView,以便每个 MediaItem 根据媒体项类型在单独的子树中结束。这可以吗?如果可以,我该怎么做?
在我的 Xaml 代码中,我假设我已将列表定义为上下文中名为 MediaItems 的属性。
<Grid>
<TreeView ItemsSource="{Binding Path=MediaItems}">
</Grid>
树视图应该是这样的
Book
- In to the wild
- Code Complete
CD
- Foo Fighters
- Bach
DVD
- X-men
- Casino Royale
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 MediaItem{
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没看出有什么问题。
在模型中,您有
MediaItem
,在模型视图中,您必须有MediaItemView
类型,如下所示:在该类上定义绑定,并定义一个
Converter
它将把enum
值转换为其string
表示形式。Don't see any problem.
In model you have
MediaItem
, on model view you have to haveMediaItemView
type, something like this:Define bindings on that class, and define a
Converter
which will convertenum
value to itsstring
presentation.我不知道树视图,但您可以使用 MediaItemType 属性上的 GroupDescription 分组和 ListBox 或 ListView 来实现类似于您所描述的内容,如所述此处
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