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.