Skip to main content

Microsoft Silverlight

Unanswered Question How to bind a Binding or a ValueConverter?RSS Feed

(0)

stefancr@scip.be
stefancr...

Member

Member

53 points

38 Posts

How to bind a Binding or a ValueConverter?

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

davidezordan
davidezo...

Contributor

Contributor

5614 points

863 Posts

Silverlight MVP

Re: How to bind a Binding or a ValueConverter?

Hi,

these articles can help you:

Thanks, Davide

Silverlight MVP

Blog Twitter Silverlight Experts

stefancr@scip.be
stefancr...

Member

Member

53 points

38 Posts

Re: How to bind a Binding or a ValueConverter?

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
EliteMike

Member

Member

65 points

25 Posts

Re: How to bind a Binding or a ValueConverter?

Anytime i need extra data in the value convertor, I use the converterparameter and pass an object in.

FuryDiamond
FuryDiamond

Contributor

Contributor

3161 points

631 Posts

Re: How to bind a Binding or a ValueConverter?

You can bind the object (rather than the individual values) and have the converter parse the needed values.

Please "Mark as Answer" if this post answered your question. :)

davidezordan
davidezo...

Contributor

Contributor

5614 points

863 Posts

Silverlight MVP

Re: How to bind a Binding or a ValueConverter?

Have you downloaded Prism? http://compositewpf.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=23450

After installed, you can find some examples in the "QuickStart" folder.

Thanks, Davide

Silverlight MVP

Blog Twitter Silverlight Experts
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities