在添加ItemsControl项目时访问它们 [英] Accessing an ItemsControl item as it is added

查看:177
本文介绍了在添加ItemsControl项目时访问它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上一篇文章中,我被建议使用DataTemplate来渲染一组按钮和标签,它的工作原理很好。问题是我有几个这些按钮组,我想区分一个组与另一个组。我打算使用AlternatingIndex来对每个组进行不同的颜色,但这还不够 - 每个组实际上还需要打印其索引。

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

我的ItemsControl(ListBox)被绑定到代码隐藏列表中。在SO上做了一些研究之后,似乎我需要以某种方式绑定ItemsControl.Count属性。一个想法是通过一个IValueConverter传递内容。内容将被数据绑定到ItemsControl.Count。然后,IValueConverter将格式化为Safe#{0}的字符串。

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}".

这是我再次震撼的数据绑定部分。这个ItemsControl的DataContext是我的ViewModel ...所以我只能猜测我需要指定一个绑定,将给我的ItemsControl而不是ViewModel。

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?

推荐答案

提供一个绑定属性,代表项目的索引集合,将 AlternationCount 属性设置为一些巨大的值(大于集合中最大可能的项数),那么可以从数据模板绑定到它: / p>

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

此外,您将不得不调整您的交替计数转换器来执行代码中的模数,因为您不再骑自行车索引自动(因为 AlternateCount 的大值)。

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

这篇关于在添加ItemsControl项目时访问它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆