views:

28

answers:

1

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

A: 

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

karmicpuppet
Bad idea actually. Backend should not rely on fronted -- it is a fronted deal to rely on backend. So backend should expose the "pure" value, it is responsibility of the fronted to present it in readable manner. Exactly as it is in my code now.
macias
ViewModels are not backend. They are part of the presentation layer. They are the "model of the view" and contain whatever it is that the View needs to present, including converted data. Here's some more information on this: http://groups.google.com/group/wpf-disciples/browse_thread/thread/3fe270cd107f184f?pli=1.
karmicpuppet
I know, what I don't know how it is suppose to make my code cleaner -- to display the values all I did was adding two lines of code (1 line -- convert, 2 line -- convert back). Adding middle layer of view model only adds complexity because now I have to duplicate all data access I have in backend, only converted. Abstractions and complex architecture are good if they serve a purpose, it is not the case here.
macias
Ok. Well, this MVVM thing is a different discussion altogether. But you know your requirements more than I do. So yeah, in your case, maybe it is better not to use it.
karmicpuppet
I added some code -- any approach to prove it worthiness has to be more compact. IOW I am convinced by the facts, not by hype of new cool abstractions :-) If it shorter, I am interested, if not -- I am not :-)
macias