Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: applying a converter to ItemsSource

Tags:

c#

.net

binding

wpf

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}}" 
like image 612
Sonic Soul Avatar asked Dec 02 '25 06:12

Sonic Soul


2 Answers

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.

like image 200
madcyree Avatar answered Dec 04 '25 21:12

madcyree


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.

like image 39
H.B. Avatar answered Dec 04 '25 20:12

H.B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!