Reputation: 1159
I'm trying to modify a dual handle slider to a generalised double model.
[UPDATED]: XAML Code:
<Slider x:Name="LowerSlider" DataContext="this" Template="{StaticResource simpleSlider}" Margin="10,0,0,0"
IsEnabled="{Binding Path=IsUpperSliderEnabled}"
Minimum="{Binding Path=Minimum}"
Maximum="{Binding Path=Maximum}"
Value="{Binding Path=LowerValue}"
SmallChange="{Binding Path=SmallChange}"
LargeChange="{Binding Path=LargeChange}" />
<Slider x:Name="UpperSlider" DataContext="this" Template="{StaticResource simpleSlider}" Margin="10,0,0,0"
IsEnabled="{Binding Path=IsUpperSliderEnabled}"
Minimum="{Binding Path=Minimum}"
Maximum="{Binding Path=Maximum}"
Value="{Binding Path=UpperValue}"
SmallChange="{Binding Path=SmallChange}"
LargeChange="{Binding Path=LargeChange}" />
C# code-behind:
#region Dependency Property - Minimum
public Double Minimum
{
get { return (Double)GetValue(MinimumProperty); }
set { SetValue(MinimumProperty, value); }
}
public static readonly DependencyProperty MinimumProperty =
DependencyProperty.Register("Minimum", typeof(Double), typeof(DualHandleSlider), new UIPropertyMetadata(0.0));
#endregion
#region Dependency Property - Lower Value
public Double LowerValue
{
get { return (Double)GetValue(LowerValueProperty); }
set { SetValue(LowerValueProperty, value); }
}
public static readonly DependencyProperty LowerValueProperty =
DependencyProperty.Register("LowerValue", typeof(Double), typeof(DualHandleSlider), new UIPropertyMetadata(0.0));
#endregion
#region Dependency Property - Upper Value
public Double UpperValue
{
get { return (Double)GetValue(UpperValueProperty); }
set { SetValue(UpperValueProperty, value); }
}
public static readonly DependencyProperty UpperValueProperty =
DependencyProperty.Register("UpperValue", typeof(Double), typeof(DualHandleSlider), new UIPropertyMetadata(5.0, new PropertyChangedCallback(OnUpperValueChanged)));
public static void OnUpperValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
#endregion
#region Dependency Property - Maximum
public Double Maximum
{
get { return (Double)GetValue(MaximumProperty); }
set { SetValue(MaximumProperty, value); }
}
public static readonly DependencyProperty MaximumProperty =
DependencyProperty.Register("Maximum", typeof(Double), typeof(DualHandleSlider), new UIPropertyMetadata(10.0, new PropertyChangedCallback(OnMaximumChanged)));
public static void OnMaximumChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
DualHandleSlider slider = (DualHandleSlider)d;
if (slider.IsUpperValueLockedToMax)
{
slider.UpperValue = (Double)e.NewValue;
}
}
#endregion
#region Dependency Property - Small Change
public double SmallChange
{
get { return (double)GetValue(SmallChangeProperty); }
set { SetValue(SmallChangeProperty, value); }
}
// Using a DependencyProperty as the backing store for SmallChange. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SmallChangeProperty =
DependencyProperty.Register("SmallChange", typeof(double), typeof(DualHandleSlider),
new UIPropertyMetadata(1.0, new PropertyChangedCallback(OnSmallChangePropertyChanged)));
protected static void OnSmallChangePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.NewValue);
}
#endregion
#region Dependency Property - Large Change
public double LargeChange
{
get { return (double)GetValue(LargeChangeProperty); }
set { SetValue(LargeChangeProperty, value); }
}
// Using a DependencyProperty as the backing store for LargeChange. This enables animation, styling, binding, etc...
public static readonly DependencyProperty LargeChangeProperty =
DependencyProperty.Register("LargeChange", typeof(double), typeof(DualHandleSlider),
new UIPropertyMetadata(1.0, new PropertyChangedCallback(OnLargeChangePropertyChanged)));
protected static void OnLargeChangePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.NewValue);
}
#endregion
The problem is, the control does not react in any way to properties set when the control is implemented. Even programmatically editing the properties does not work? I figure it's a stupid mistake, but I've been pouring over this code for a while now. Any ideas?
EDIT: Still no luck! Nothing in output about binding errors.
Upvotes: 2
Views: 291
Reputation: 64068
You were close with your initial code prior to the latest edit. The key is to name your user control and to bind against the user control. This allows the DataContext
to flow through naturally if you ever support embedded ContentControl
s. Setting DataContext="this"
literally sets your data context to the string "this"
.
Instead add something like x:Name="ThisControl"
to your <UserControl...
line and then update the bindings to point to ElementName=ThisControl
. Make sure the Slider.Value
bindings are Mode=TwoWay
(which is the default if you don't specify a mode):
<UserControl x:Class="NameSpace.ThisControl"
x:Name="ThisControl">
<Grid>
<Slider x:Name="LowerSlider"
IsEnabled="{Binding IsUpperSliderEnabled, ElementName=ThisControl}"
Minimum="{Binding Minimum, ElementName=ThisControl}"
Maximum="{Binding Maximum, ElementName=ThisControl}"
Value="{Binding LowerValue, ElementName=ThisControl}"
SmallChange="{Binding SmallChange, ElementName=ThisControl}"
LargeChange="{Binding LargeChange, ElementName=ThisControl}" />
<Slider x:Name="UpperSlider"
IsEnabled="{Binding IsUpperSliderEnabled, ElementName=ThisControl}"
Minimum="{Binding Minimum, ElementName=ThisControl}"
Maximum="{Binding Maximum, ElementName=ThisControl}"
Value="{Binding UpperValue, ElementName=ThisControl}"
SmallChange="{Binding SmallChange, ElementName=ThisControl}"
LargeChange="{Binding LargeChange, ElementName=ThisControl}" />
Upvotes: 2
Reputation: 21855
Are you setting the control DataContext property to the right value? You can see the output window to see the Binding errors you may have.
EDIT: Using ElementName=root you're directing your binding to something called root. The normal way of doing this (IMHO) is to delete the ElementName part of all the bindings and setting the DataContext = this to be able to bind to its own DP.
Upvotes: 1