添加 ItemsControl 项时访问它

发布于 2024-08-17 01:14:17 字数 828 浏览 3 评论 0原文

在上一篇文章中,我被建议使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林迷了鹿 2024-08-24 01:14:17

要提供表示集合中项目索引的绑定属性,请将 AlternationCount 属性设置为某个巨大值(大于集合中项目的最大可能数量),然后就可以绑定因此

{Binding RelativeSource={RelativeSource TemplatedParent},
         Path=TemplatedParent.(ItemsControl.AlternationIndex)}

,您将必须调整交替计数转换器以在代码中执行模数,因为您不再自动循环索引(因为 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:

{Binding RelativeSource={RelativeSource TemplatedParent},
         Path=TemplatedParent.(ItemsControl.AlternationIndex)}

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).

终陌 2024-08-24 01:14:17

@Aviad:谢谢,我会尝试一下!只是为了完整起见,我想发布我刚刚尝试过的内容。我终于让数据绑定按照我提议的方式工作:

<Label Grid.Row="1" Grid.Column="0" Content="{Binding Path=Items.Count, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}"></Label>

最终结果是错误的 - 所有 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:

<Label Grid.Row="1" Grid.Column="0" Content="{Binding Path=Items.Count, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}"></Label>

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文