WPF MultiBindings

Fadi Al Sayyed picture Fadi Al Sayyed · Mar 1, 2010 · Viewed 8.1k times · Source

I need to implement MultiBindings in C# directly without using XAML, I know how to use the IMultiValueConverter in C#, but, how to do:

<MultiBinding Converter="{StaticResource sumConverter}">
  <Binding ElementName="FirstSlider" Path="Value" />
  <Binding ElementName="SecondSlider" Path="Value" />
  <Binding ElementName="ThirdSlider" Path="Value" />
</MultiBinding>

in C# ?

Answer

Matthias picture Matthias · Mar 1, 2010

Why not using XAML?

The following code should work:

MultiBinding multiBinding = new MultiBinding();

multiBinding.Converter = converter;

multiBinding.Bindings.Add(new Binding
{
    ElementName = "FirstSlider",
    Path = new PropertyPath("Value")
});
multiBinding.Bindings.Add(new Binding
{
    ElementName = "SecondSlider",
    Path = new PropertyPath("Value")
});
multiBinding.Bindings.Add(new Binding
{
    ElementName = "ThirdSlider",
    Path = new PropertyPath("Value")
});