title: Two way binding of a textbox to a slider in WPF
body: <p>I am having the hardest time figuring out a way to solve a problem I am having with databinding on a slider and a textbox.</p>
  
  <p><strong>The setup</strong>: 
  the current value of the slider is displayed inside of the textbox. When the user drags the slider the value is reflected inside the textbox. The user can choose to drag the slider and release to the value he chooses, click anywhere on the slider track to set the value or enter the value manually in the texbox. In the last case, the value entered in the textbox should update the slider position.</p>
  
  <p>The texbox is two way bound to a datacontext property while the slider is one way bound to the same property. When the user slides or click on the slider tracker, I use the dragcompleted event of the slider to notify the datacontext of the modification. When the user clicks on the tracker on the other hand I use the OnValueChanged event of the slider to notify the datacontext (and use a flag to ensure the OnValueChanged was not triggered by a slider move)</p>
  
  <p><strong>The problem</strong>: The OnValueChanged event fires even when initializing the slider value with the binding value so I cannot figure out whether the value is actually coming from the user or the binding.</p>
  
  <p>Could you please suggest maybe and alternative way to do the binding to ensure we can distinguish between user update and binding udpates for the slider?
  Thnak you!</p>
  
  <p><strong>UPDATE</strong> Sorry I forgot to mention why I am not binding directly both slider and textbox two ways like the below answers suggest. The update to the data context value is supposed to trigger a call to a backend server and retrieve data from a database. The problem is that when the user drags the slider it constantly fires updates. I go around the problem by only relying to the actual onValueChanged event to call the DoWhatever method. I hope that's a bit clearer. Sorry for omitting this...</p>
  
  <p>I quickly put together the sample below for you to give it a try.</p>
  
  <p><em>The xaml</em>:</p>
  
  <pre><code>&lt;Window x:Class="SliderIssue.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Grid HorizontalAlignment="Center"
        VerticalAlignment="Center"&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition/&gt;
          &lt;ColumnDefinition/&gt;
      &lt;/Grid.ColumnDefinitions&gt;
  
      &lt;Slider Name="slider" VerticalAlignment="Top"  
              ValueChanged="slider_ValueChanged"
              Thumb.DragStarted="slider_DragStarted"
              Thumb.DragCompleted="slider_DragCompleted"
              Value="{Binding Count}"
              Width="200"
              Minimum="0"
              Maximum="100"/&gt;
      &lt;TextBox VerticalAlignment="Top" 
               HorizontalAlignment="Left"
               Grid.Column="1" 
               Width="100" 
               Text="{Binding Count,Mode=TwoWay,UpdateSourceTrigger=LostFocus}" 
               Height="25"/&gt;    
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p><em>The code behind:</em></p>
  
  <pre><code>using System.Windows;
  
  namespace SliderIssue
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          private bool _dragStarted;
  
          public MainWindow()
          {
              InitializeComponent();
  
              var item = new Item();
              DataContext = item;
          }
  
          private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs&lt;double&gt; e)
          {
              if (!_dragStarted)
              {
                  var item = (Item)DataContext;
                  item.DoWhatever(e.NewValue);
              }
          }
  
          private void slider_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
          {
              _dragStarted = true;
          }
  
          private void slider_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
          {
              _dragStarted = false;
  
              var item = (Item) DataContext;
              item.DoWhatever(slider.Value);
          }
      }
  }
  </code></pre>
  
  <p><em>A simple data class:</em></p>
  
  <pre><code>using System.ComponentModel;
  
  namespace SliderIssue
  {
      public class Item : INotifyPropertyChanged
      {
          private int _count = 50;
          public int Count
          {
              get { return _count; }
              set
              {
                  if (_count != value)
                  {
                      _count = value;
                      DoWhatever(_count);
                      OnPropertyChanged("Count");
                  }
              }
          }
  
          public void DoWhatever(double value)
          {
              //do something with value
              //and blablabla
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          protected void OnPropertyChanged(string property)
          {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(property));
              }
          }
      }
  }
  </code></pre>