1

I have a custom user control (SliderControl.xaml) that is composed of a slider control and a few other controls. The user control has a dependency property for the Value property of the slider control.

public static DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(SliderControl), new PropertyMetadata(50.0));

public double Value {
   get { return (double)GetValue(ValueProperty); }
   set { SetValue(ValueProperty, (double)value); }
}

I have a window that is using my custom slider control inside of a StackPanel like thus:

<StackPanel>
   <cl:SliderControl x:Name="valvePosUC" Minimum="0" Maximum="100" Title="Valve Position" Value="50" />
</StackPanel>

This works just fine. The '50' in the Value dependency property is properly setting the slider control to a value of 50.

However, what I would like to do is bind the Value dependency property to a property exposed in the view model to which my window is currently bound.

Here is the DataContext being set in my window.

<Window.DataContext>
   <ViewModel:LRS_1920x1080VM/>
</Window.DataContext>

And the property in my view model (LRS_1920x1080VM) that I would like to bind the Value dependency property is defined like such:

private double _valvePos_SliderValue { get; set; }
public double ValvePos_SliderValue {
   get { return _valvePos_SliderValue; }
   set {
      _valvePos_SliderValue = value;
      OnPropertyChanged("ValvePos_SliderValue");
   }
}

So what I would like to be able to do is this (note the Value binding below):

<StackPanel>
   <cl:SliderControl x:Name="valvePosUC" Minimum="0" Maximum="100" Title="Valve Position" Value="{Binding ValvePos_SliderValue}" />
</StackPanel>

However, when I try to bind the Value dependency property like such, it doesn't work. The error that I see in my output says --

System.Windows.Data Error: 40 : BindingExpression path error: 'ValvePos_SliderValue' property not found on 'object' ''SliderControl' (Name='valvePosUC')'. BindingExpression:Path=ValvePos_SliderValue; DataItem='SliderControl' (Name='valvePosUC'); target element is 'SliderControl' (Name='valvePosUC'); target property is 'Value' (type 'Double')

The error seems to indicate that it is trying to find the property ValvePos_SliderValue in the SliderControl, which is not where it is. The ValvePos_SliderValue is a property in the view model.

Jagd
  • 7,169
  • 22
  • 74
  • 107
  • Do you set the DataContext of the SliderControl to itself somewhere? – mm8 Feb 28 '17 at 16:42
  • Besides the problem with the DataContext, you should also make sure that the Binding to your UserControl's Value property is two-way. Either explicitly set the Mode in the Binding expression, or register the dependency property with the `BindsTwoWayByDefault` flag. – Clemens Feb 28 '17 at 16:54
  • @Clemens - thanks. I'll do that too. – Jagd Feb 28 '17 at 16:55

1 Answers1

2

Do you set the DataContext of the SliderControl to itself somewhere?

Try to specify the source of the binding explicitly:

<cl:SliderControl x:Name="valvePosUC" Minimum="0" Maximum="100" Title="Valve Position" 
              Value="{Binding DataContext.ValvePos_SliderValue, RelativeSource={RelativeSource AncestorType=Window}}" />
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Yes! Wow, I get it now. In the header of the SliderControl the DataContext is being set to itself. So is there some way to get around this and still allow me to bind the Value dependency property to a property on a view model? – Jagd Feb 28 '17 at 16:47
  • Did you try to specify the source of the property using a RelativeSource as per my answer (just copy and paste it :)? This should work provided that the SliderControl is a visual child of the window of which you set the DataContext to an instance of your view model class. – mm8 Feb 28 '17 at 16:48
  • It worked! I had tried a similar binding to what you had before, but I didn't have the "DataContext." in it, and I think that's why it wasn't working. Thanks for the help!! – Jagd Feb 28 '17 at 16:53