Skip to main content
Home Forums Silverlight Programming Programming with .NET - General How to bind a Binding or a ValueConverter?
5 replies. Latest Post by davidezordan on May 27, 2009.
(0)
stefancr...
Member
53 points
38 Posts
05-18-2009 7:29 AM |
I’m always struggling with ValueConverters because the ConvertParameter is not a dependency property and so it doesn’t support databinding.I was trying to solve my problem by creating a derived ValueConverter with some extra properties. The ValueConverter is created in code (in a ViewModel class) and the extra properties are initialized.Because I’m using the M-V-VM pattern I would like to bind the new converter to an existing binding or create a binding in run-time and bind it to a UI element. Something like this:
<dataControls:DataFormComboBoxField Binding="{Binding Id, Mode=TwoWay, Convert={Binding MyConverter}}"</dataControls:DataFormComboBoxField>Or <dataControls:DataFormComboBoxField Binding="{Binding MyBindingWithConverter}"</dataControls:DataFormComboBoxField>Is there a way to bind a Binding or a ValueConverter in XAML? Can binding be used in static resources? All tips and ideas are appreciated.Stefan
davidezo...
Contributor
5614 points
863 Posts
05-18-2009 9:06 AM |
Hi,
these articles can help you:
05-19-2009 10:12 AM |
Thanks Davide.I know all the basics about binding, valueconverters and static resources, but I'm looking for a way to pass extra data to a value converter. So I was hoping that there would a way to bind a Binding object or a ValueConverter object.
e.g. a Binding object and a ValueConverter object are created in run-time and set in the code behind of the XAML file.
var myConverter = new ComboBoxLookupValueConverter(customers); customerCombo.Binding.Converter = myConverter;var myBinding = new Binding() { Source = customer, Path = new PropertyPath("CustomerId"), Converter = new ComboBoxLookupValueConverter(customers) };
customerCombo.Binding = myBinding ;
This works fine but I'm using a ViewModel so I would like to bind these objects via public properties. Something like
<dataControls:DataFormComboBoxField Binding="{Binding CustomerId, Mode=TwoWay, Converter={Binding MyConverter}}"</dataControls:DataFormComboBoxField>Or <dataControls:DataFormComboBoxField Binding="{Binding MyBindingWithConverter}"</dataControls:DataFormComboBoxField>
Is there a way to do this? Or could I use static resources and use bindings in these resources? Or has anyone created a derived Binding object which has dependency properties for extra parameters?
EliteMike
65 points
25 Posts
05-19-2009 11:53 AM |
Anytime i need extra data in the value convertor, I use the converterparameter and pass an object in.
FuryDiamond
3161 points
631 Posts
05-19-2009 12:26 PM |
You can bind the object (rather than the individual values) and have the converter parse the needed values.
05-27-2009 12:20 PM |
Have you downloaded Prism? http://compositewpf.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=23450
After installed, you can find some examples in the "QuickStart" folder.