添加 ItemsControl 项时访问它
在上一篇文章中,我被建议使用 DataTemplate 来渲染一组按钮和标签,它的效果非常好。问题是我有几组按钮,我想将一组与另一组区分开来。我计划使用 AlternatingIndex 为每个组设置不同的颜色,但这还不够 - 每个组实际上还需要打印其索引。
这是一个人为的示例...假设该项目看起来像这样:
Lock Door
Safe Unlock Door
Sound Alarm
如果我的房间里装满了这些保险箱,我想知道我正在访问哪一个。因此,我希望列表看起来像这样:
Lock Door
Safe #1 Unlock Door
Sound Alarm
Lock Door
Safe #2 Unlock Door
Sound Alarm
我的 ItemsControl (ListBox) 绑定到代码隐藏中的列表。在对 SO 进行了一些研究之后,似乎我需要以某种方式绑定 ItemsControl.Count 属性。我的一个想法是通过 IValueConverter 传递内容。内容将数据绑定到 ItemsControl.Count。然后 IValueConverter 会将字符串格式化为“Safe #{0}”。
这是我再次犹豫不决的数据绑定部分。此 ItemsControl 的 DataContext 是我的 ViewModel...所以我只能猜测我需要指定一个 Binding 来为我提供 ItemsControl 而不是 ViewModel。
这是正确的想法吗?如果是这样,有人可以帮我绑定吗?如果没有,我还可以尝试哪些其他方法?
In a previous post, I was advised to use a DataTemplate to render a group of Buttons and Labels, and it works wonderfully. The problem is that I have several of these groups of Buttons, and I would like to distinguish one group from another. I plan to use the AlternatingIndex to color each group differently, but that's not enough -- each group actually needs to have its index printed as well.
Here's a contrived example... let's say the Item looks something like this:
Lock Door
Safe Unlock Door
Sound Alarm
If I have a room full of these safes, I'd like to know which one I'm accessing. Therefore, I'd like the list to look like this:
Lock Door
Safe #1 Unlock Door
Sound Alarm
Lock Door
Safe #2 Unlock Door
Sound Alarm
My ItemsControl (ListBox) is bound to a List in code-behind. After doing some research here on SO, it seems like I need to somehow bind the ItemsControl.Count property. One idea I had was to pass the Content through an IValueConverter. The Content would be databound to ItemsControl.Count. Then the IValueConverter would just format the string to be "Safe #{0}".
It's the databinding part that I'm once again faltering on. The DataContext for this ItemsControl is my ViewModel... so I can only guess that I need to specify a Binding that will give me the ItemsControl instead of the ViewModel.
Is this the right idea? If so, can someone help me with the Binding? If not, what other methods might I try?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要提供表示集合中项目索引的绑定属性,请将
AlternationCount
属性设置为某个巨大值(大于集合中项目的最大可能数量),然后就可以绑定因此,您将必须调整交替计数转换器以在代码中执行模数,因为您不再自动循环索引(因为
AlternationCount
)。To provide a property for binding that represents the index of the item in the collection, set the
AlternationCount
property to some huge value (larger than the maximum possible number of items in the collection), then you can bind to it from your data template thus:Also, you will have to tweak your alternation count converter to do the modulus in code, since you're no longer cycling the index automatically (because of the big value of
AlternationCount
).@Aviad:谢谢,我会尝试一下!只是为了完整起见,我想发布我刚刚尝试过的内容。我终于让数据绑定按照我提议的方式工作:
最终结果是错误的 - 所有 ListBox 项目都有索引“4”,所以我猜所有标签的内容在项目添加到后都会被评估容器。有趣的!
@Aviad: thanks, I'll try that! Just for completion's sake, I wanted to post what I had just tried. I finally got the databinding to work the way I had proposed:
The end result was wrong -- all of the ListBox items had the index "4", so I guess the Content of all of the Labels gets evaluated after the items are added to the container. Interesting!