1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

DraggableNumberBox - Fixed min and max value

This commit is contained in:
Robert 2022-07-28 21:09:48 +02:00
parent 4703c0890a
commit 0e3d26e89e
3 changed files with 43 additions and 8 deletions

View File

@ -17,7 +17,7 @@
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.10.15" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="0.10.15"/>
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="0.10.15" />
<PackageReference Include="Avalonia.ReactiveUI" Version="0.10.15" />
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="0.10.14" />
<PackageReference Include="DynamicData" Version="7.8.6" />

View File

@ -25,15 +25,13 @@
<Panel>
<controls:NumberBox Name="NumberBox"
AcceptsExpression="True"
Value="{Binding $parent[sharedControls:DraggableNumberBox].Value}"
Minimum="{Binding $parent[sharedControls:DraggableNumberBox].Minimum}"
Maximum="{Binding $parent[sharedControls:DraggableNumberBox].Maximum}"
LargeChange="{Binding $parent[sharedControls:DraggableNumberBox].LargeChange}"
SmallChange="{Binding $parent[sharedControls:DraggableNumberBox].SmallChange}"
SimpleNumberFormat="{Binding $parent[sharedControls:DraggableNumberBox].SimpleNumberFormat}"
attachedProperties:NumberBoxAssist.PrefixText="{Binding $parent[sharedControls:DraggableNumberBox].Prefix}"
attachedProperties:NumberBoxAssist.SuffixText="{Binding $parent[sharedControls:DraggableNumberBox].Suffix}"
HorizontalAlignment="{Binding $parent[sharedControls:DraggableNumberBox].HorizontalAlignment}" />
HorizontalAlignment="{Binding $parent[sharedControls:DraggableNumberBox].HorizontalAlignment}"
ValueChanged="NumberBox_OnValueChanged"/>
<Rectangle Name="DragCollider" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Fill="Transparent"></Rectangle>
</Panel>

View File

@ -19,17 +19,31 @@ public class DraggableNumberBox : UserControl
/// <summary>
/// Defines the <see cref="Value" /> property.
/// </summary>
public static readonly StyledProperty<double> ValueProperty = AvaloniaProperty.Register<DraggableNumberBox, double>(nameof(Value), defaultBindingMode: BindingMode.TwoWay);
public static readonly StyledProperty<double> ValueProperty = AvaloniaProperty.Register<DraggableNumberBox, double>(nameof(Value), defaultBindingMode: BindingMode.TwoWay, notifying: ValueChanged);
private static void ValueChanged(IAvaloniaObject sender, bool before)
{
if (before)
return;
DraggableNumberBox draggable = (DraggableNumberBox) sender;
if (!(Math.Abs(draggable._numberBox.Value - draggable.Value) > 0.00001))
return;
draggable._updating = true;
draggable._numberBox.Value = draggable.Value;
draggable._updating = false;
}
/// <summary>
/// Defines the <see cref="Minimum" /> property.
/// </summary>
public static readonly StyledProperty<double> MinimumProperty = AvaloniaProperty.Register<DraggableNumberBox, double>(nameof(Minimum));
public static readonly StyledProperty<double> MinimumProperty = AvaloniaProperty.Register<DraggableNumberBox, double>(nameof(Minimum), double.MinValue);
/// <summary>
/// Defines the <see cref="Maximum" /> property.
/// </summary>
public static readonly StyledProperty<double> MaximumProperty = AvaloniaProperty.Register<DraggableNumberBox, double>(nameof(Maximum));
public static readonly StyledProperty<double> MaximumProperty = AvaloniaProperty.Register<DraggableNumberBox, double>(nameof(Maximum), double.MaxValue);
/// <summary>
/// Defines the <see cref="LargeChange" /> property.
@ -61,6 +75,7 @@ public class DraggableNumberBox : UserControl
private double _lastX;
private bool _moved;
private double _startX;
private bool _updating;
/// <summary>
/// Creates a new instance of the <see cref="DraggableNumberBox" /> class.
@ -69,6 +84,7 @@ public class DraggableNumberBox : UserControl
{
InitializeComponent();
_numberBox = this.Get<NumberBox>("NumberBox");
_numberBox.Value = Value;
PointerPressed += OnPointerPressed;
PointerMoved += OnPointerMoved;
@ -237,4 +253,25 @@ public class DraggableNumberBox : UserControl
e.Handled = true;
}
private void NumberBox_OnValueChanged(NumberBox sender, NumberBoxValueChangedEventArgs args)
{
if (_updating)
return;
if (args.NewValue < Minimum)
{
_numberBox.Value = Minimum;
return;
}
if (args.NewValue > Maximum)
{
_numberBox.Value = Maximum;
return;
}
if (Math.Abs(Value - _numberBox.Value) > 0.00001)
Value = _numberBox.Value;
}
}