这是xaml的代码段
我有一些RangeSliders,我想将上限和下限值绑定到我的VM。通常,我会像下面这样尝试,但是每个元素名称有两个不同的值,如何处理这种情况?我是新来的!
<StackPanel Grid.Row="1" VerticalAlignment="Bottom" Width="210" Grid.ColumnSpan="2" Grid.Column="1">
<local:RangeSlider x:Name="sliderWind"
Margin="10"
LowerValue="70"
UpperValue="120"
Minimum="0"
Maximum="160"
/>
</StackPanel>
<Label Content ="Wind Speed" Grid.Column="0" Grid.Row="1" HorizontalAlignment ="Right" Margin="0,0,0,0" Height="auto" Width ="auto" FontSize="20" FontWeight="Bold" VerticalAlignment="Bottom" />
<TextBox Text="{Binding ElementName=sliderWind, Path=LowerValue, StringFormat=N2}" HorizontalAlignment="Left" Grid.Row="1" Grid.Column="1" Margin="1,1,1,1" VerticalAlignment="Bottom" Width="50" Height="30" FontWeight="Bold" />
<TextBox Text="{Binding ElementName=sliderWind, Path=UpperValue, StringFormat=N2}" HorizontalAlignment="Right" Grid.Row="1" Grid.Column="2" Margin="1,1,1,1" VerticalAlignment="Bottom" Width="50" Height="30" FontWeight="Bold" />
在Rangeslider后面查看
public partial class RangeSlider : UserControl
{
public RangeSlider()
{
InitializeComponent();
this.Loaded += Slider_Loaded;
}
void Slider_Loaded(object sender, RoutedEventArgs e)
{
LowerSlider.ValueChanged += LowerSlider_ValueChanged;
UpperSlider.ValueChanged += UpperSlider_ValueChanged;
}
private void LowerSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
UpperSlider.Value = Math.Max(UpperSlider.Value, LowerSlider.Value);
}
private void UpperSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
LowerSlider.Value = Math.Min(UpperSlider.Value, LowerSlider.Value);
}
public double Minimum
{
get { return (double)GetValue(MinimumProperty); }
set { SetValue(MinimumProperty, value); }
}
public static readonly DependencyProperty MinimumProperty =
DependencyProperty.Register("Minimum", typeof(double), typeof(RangeSlider), new UIPropertyMetadata(0d));
public double LowerValue
{
get { return (double)GetValue(LowerValueProperty); }
set { SetValue(LowerValueProperty, value); }
}
public static readonly DependencyProperty LowerValueProperty =
DependencyProperty.Register("LowerValue", typeof(double), typeof(RangeSlider), new UIPropertyMetadata(0d));
public double UpperValue
{
get { return (double)GetValue(UpperValueProperty); }
set { SetValue(UpperValueProperty, value); }
}
public static readonly DependencyProperty UpperValueProperty =
DependencyProperty.Register("UpperValue", typeof(double), typeof(RangeSlider), new UIPropertyMetadata(0d));
public double Maximum
{
get { return (double)GetValue(MaximumProperty); }
set { SetValue(MaximumProperty, value); }
}
public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(RangeSlider), new UIPropertyMetadata(1d));
这通常是我在VM中捕获值/值更改的方式
private int _LowerValue;
public int LowerValue
{
get { return _LowerValue; }
set
{
if (value != _LowerValue)
{
_LowerValue = value;
OnPropertyChanged(nameof(LowerValue));
}
}
}