3

is it possible to apply a converter to a data source of a control in xaml?

or perhaps there is another way to do this.

Basically i have a custom control that accepts a specific type of object. that object is tightly bound to that control. I don't want to convert to this type all over my view model. So i would like to be able to bind to regular properties such as List and have it automatically translated to my object by a converter.

I've attempted something like this.

ItemsSource="{Binding CurrentTables, Converter={x:Static cconverters:SpyFilterDataObjectConverter}}" 
Sonic Soul
  • 23,855
  • 37
  • 130
  • 196

2 Answers2

1

x:Static has the syntax namespace:Type.StaticMember, you should instantiate the converter and expose it as a static property.

Alternatively you can create an instance in the Application.Resources in your App.xaml, then you can reference it as a static resource throughout the application using its key.

H.B.
  • 166,899
  • 29
  • 327
  • 400
1

Well, it doesn't seem good as for me to use such kind of converters. Basically, converter performs conversion operation only once, so you will not receive any updates. I've used different approach - just create some sort of wrapper that contains an initial collection (it should implement INotifyCollectionChanged) and some wrap strategies that converts your initial object to wrapped one.

madcyree
  • 1,427
  • 3
  • 15
  • 28
  • that would be great.. but i am using MVVM.. so my control is bound to a dependency property on the viewmodel. not sure how to inject a wrapper in the middle of that binding.. so that it reacts to any changes of the property.. – Sonic Soul May 21 '11 at 17:10
  • You should not pass this wrapper to the binding, just create an instance of wrapper, pass your collection into this wrapper and bind your property to the wrapper. Using this approach, the wrapper will encapsulate all conversion logic like a black box. See what I mean? BTW, do you know that your viewmodel can implement INotifyPropertyChanged? It means that you can exclude DependencyObject from your presenters hierarchy. – madcyree May 23 '11 at 14:36
  • if i was passing my collection using code, then all of this would be pretty simple to solve. My difficulty stems from the fact i am using MVVM, and i am simply binding to properties on the view model, which are standard lists of string/object etc.. i want to seamlessly translate those objects to a specific type every time the list changes – Sonic Soul May 23 '11 at 14:46
  • how does using INotifyPropertyChanged over dependency properties help solve this problem? – Sonic Soul May 23 '11 at 14:49
  • With respect to the question about INotifyPropertyChanged - this using can't help you to solve aforementioned problem. Just a recommendation, nothing more. Sorry, I still do not understand, why don't you just create a wrapped I describe, and aggregate this wrapper by your viewmodel.... – madcyree May 23 '11 at 15:16