在 XAML 中呈现分隔列表?
是否有通用的 XAML 样式或模板技术来将项目的数据绑定列表呈现为屏幕上的分隔列表?
想象一下在 XAML 中输出类似于函数调用声明的内容:foo(a,b,c)
,其中“a”、“b”和“c”来自数据绑定列表。 (“foo”也是数据绑定的,但不是参数列表)
“a”、“b”和“c”元素需要保留 XAML UI 元素,以便它们可以在 XAML 中进行 UI 交互和样式设置。 (所以我认为这不符合简单字符串连接作为解决方案的资格)
关键点是列表末尾不有一个虚假的尾随逗号。
这在某种程度上与如何设置列表中第一个和/或最后一个元素与中间元素不同的样式的问题有关。我在 stackoverflow 上看到了在 ItemsControl
中使用 ItemsTemplateSelector
来测试某个项目是列表中的第一个还是最后一个项目并返回适当的模板的建议,但是问题是 DataTemplateSelector.SelectTemplate 无法访问该项目来自的列表。您可以将特定列表的肉欲知识作为一次性的拼凑,但如何以通用的、可重用的方式做到这一点呢?
Is there a general-purpose XAML styling or templating technique to render a databound list of items as a delimited list on screen?
Imagine outputing something like a function call declaration in XAML: foo(a,b,c)
where "a", "b", and "c" come from a databound list. ("foo" is databound, too, but not to the parameter list)
The "a", "b", and "c" elements need to remain XAML UI elements so they can be UI interactive and stylable in XAML. (So I think this disqualifies simple string concatenation as a solution)
The critical point is to not have a spurious trailing comma at the end of the list.
This is somewhat related to the question of how to style the first and/or last elements in a list differently from the middle elements. I've seen suggestions here on stackoverflow to use an ItemsTemplateSelector
in an ItemsControl
to test whether an item is the first or last in the list and return the appropriate template, but the problem with that is the DataTemplateSelector.SelectTemplate
has no access to the list that the item came from. You can hack carnal knowledge of a particular list as a one-off kludge, but how do you do this in a general purpose, reusable way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 WPF 中,您可以使用与相对源 PreviousData 的绑定。如果先前的数据为空,则您知道它是列表中的第一项,因此无需在前面添加分隔符。
唉,Silverlight 不支持这一点,当我有完全相同的要求时,我无法找到一个很好的解决方法。我最终只是设置了一个边距,这样后面的逗号就永远不可见。
In WPF, you can use a binding with relative source PreviousData. If previous data is null, you know it's the first item in the list, so no need to prepend a separator.
Alas, Silverlight doesn't support this and I was unable to find a nice workaround when I had the exact same requirement. I ended up just setting a margin such that the trailing comma would never be visible.