如何将单个项目转换器应用于 ItemSource 属性?
我使用转换器为 ListBox 设置了 ItemSource:
<ListBox Name="FunctionsListBox"
ItemsSource="{Binding Path=Functions,
Converter={x:Static app:CreatorWindow.FunctionConverter}}"/>
但是,这对我来说看起来很难看,因为转换器会转换整个集合 - 我更喜欢更通用的转换器,它只转换单个项目。我可以毫无问题地编写,但是如何强制 ListBox 一项一项地调用转换器,而不是一次调用所有项目?
我知道我可以更详细地为 ListBox 定义 ItemTemplate:
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Converter={x:Static app:CreatorWindow.FunctionConverter}}"/>
</DataTemplate>
</ListBox.ItemTemplate>
但这会干扰 ListBox 使用的小部件,而且它有点长。
那么如何简单地做到这一点呢?抱歉我很挑剔,我只是喜欢干净的代码:-)
离题
因为我感觉(并看到)答案可能会走向完全错误的方向,这是我的 FunctionConverter。
FunctionConverter = LambdaConverter.Create((GeneratorEnum e) => GeneratorsFactory.GeneratorNames[e],
(string s) => GeneratorsFactory.GeneratorNames[s]);
所以我有后端,我可以在其中访问数据,它与 UI 无关,而且我有 UI。为了建立后端和前端之间的链接,我使用上面的转换器。这是最少的代码方法,而且很灵活。
GeneratorNames 是具有唯一键和值的关联数组,因此您可以使用值或键作为索引来查询它。
在两者之间添加新层并不能真正解决我的问题,不能回答我的问题,即使我感兴趣,它也会添加很多代码(这正是我不感兴趣的原因 - 上面你看到 2 行,如果任何事情都更好,它必须只在一行行中)。
I set up ItemSource for the ListBox with converter:
<ListBox Name="FunctionsListBox"
ItemsSource="{Binding Path=Functions,
Converter={x:Static app:CreatorWindow.FunctionConverter}}"/>
However this looks for me ugly, because converter converts entire collection -- I would prefer more versatile converter which converts just single item. I can write without a problem, but how to force ListBox to call converter one by one, instead of all items at once?
I know I can be more elaborate and define ItemTemplate for ListBox:
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Converter={x:Static app:CreatorWindow.FunctionConverter}}"/>
</DataTemplate>
</ListBox.ItemTemplate>
But this interferes with the widget used by ListBox and it is a bit lengthy.
So how to do it -- in short manner? Sorry for being picky, I just like a clean code :-)
Off-topic
Because I sense (and see) the answers can go in completely wrong direction, this is my FunctionConverter.
FunctionConverter = LambdaConverter.Create((GeneratorEnum e) => GeneratorsFactory.GeneratorNames[e],
(string s) => GeneratorsFactory.GeneratorNames[s]);
So I have backend, where I can access data, and it is UI-agnostic, and I have UI. To establish link between backend and frontend, I use converters as above. This is minimal code approach and it is flexible.
GeneratorNames is associative array with unique keys and values, so you can query it using value or key as an index.
Adding new layer between does NOT really solve my problem, does NOT answer my question, and even if I would be interested it adds a lot of code (and that is exactly why I am not interested -- above you see 2-liner, if anything is better it has to be in only one line).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你使用MVVM吗?因为如果是的话,您始终可以在那里进行转换,并且只需公开包含转换后的项目的属性以供 UI 使用。如果你不是,我建议你尝试一下。它会让你的代码更干净。 =)
Are you using MVVM? Because if you are, you can always do your conversion there and just expose a property containing the converted items for the UI to use. If you aren't, I suggest that you try it out. It will make your code cleaner. =)