0

I'm developing a C#/WPF MVVM application and I have the following controls in my Xaml:

<TextBox Text="{Binding ElementName=PriceSlider, Path=Value, Mode=TwoWay}"/>
<Slider x:Name="PriceSlider" Minimum="0" Maximum="10000"/>

The user should be able to set the textbox's value both by typing it in and through the slider indirectly, so I need this binding between them. Moreover, I should bind the textbox's value into one of my VM's property for validation and further computation.

I thought about using the textbox's TextChanged event with the CallMethodAction of the Xaml.Behaviours package passing the EventArgs, but I guess it wouldn't be so efficient to fire events every time the slider sends a new value.

Is there a way to bind the textbox both to the slider and e.g. OneWayToSource to one of the DataContext's property? Or any other way that is sort of efficient?

Phil
  • 31
  • 4
  • 4
    No, bind the relevant properties of both UI elements to a single property in a view model. – Clemens Apr 01 '21 at 12:13
  • @SomeBody thanks for the quick answer. It's a smart solution, works great. – Phil Apr 01 '21 at 12:24
  • MultiBinding? - eg. https://stackoverflow.com/questions/2552853/how-to-bind-multiple-values-to-a-single-wpf-textblock – Rand Random Apr 01 '21 at 12:29
  • **Edit**: Idk why @SomeBody deleted his answer. His answer was: bind the `TextBox`'s `Text` to the VM's property, and bind the `Slider`'s `Value` property to the `TextBox`'s `Text`. – Phil Apr 01 '21 at 12:39
  • I guess, because it isn't necessary, just bind both the TextBox and the Slider to the same VM's property, just like Clemens recommended. – Rand Random Apr 01 '21 at 12:43
  • Alright, thank you all. – Phil Apr 01 '21 at 12:48

0 Answers0