WPF MultiBindings

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

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

in c #?

+4
source share
2 answers

Why not use 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") }); 
+4
source

There are two ways to do this on the C # side (I assume you just don't want to literally port MultiBinding to code, which is really useless if you do this, XAML is always better for this)

  • An easy way is to create a ValueChanged event handler for 3 sliders and calculate the sum there and assign the desired property.

2. The second and best way to get closer to them in WPF is to make the MVVM style of the application (I hope you know about MVVM). In your ViewModel class, you will have 3 different properties. And you need another โ€œSumโ€ property also in the class. The amount will be revalued whenever another property agent is called.

 public double Value1 { get { return _value1;} set { _value1 = value; RaisePropertyChanged("Value1"); ClaculateSum(); } } public double Sum { get { return _sum;} set { _sum= value; RaisePropertyChanged("Sum"); } } public void CalculateSum() { Sum = Value1+Value2+Value3; } 
0
source

Source: https://habr.com/ru/post/1302714/


All Articles