feat: make property context friendly.

This commit is contained in:
rabbitism
2024-01-14 23:44:23 +08:00
parent 0f3883ed66
commit 296777febb
3 changed files with 26 additions and 10 deletions

View File

@@ -14,13 +14,13 @@
</Style>
</UserControl.Styles>
<StackPanel HorizontalAlignment="Left">
<u:NumericIntUpDown Name="input" Step="1" Value="2" Watermark="Input Value" />
<u:NumericIntUpDown Name="input" Step="1" Value="2" Watermark="Input Value" IsReadOnly="True" />
<TextBlock Text="{Binding #input.Value}" ></TextBlock>
<u:NumericDoubleUpDown Name="inputDouble" Step="0.5" Value="3.1" EmptyInputValue="1"></u:NumericDoubleUpDown>
<TextBlock Text="{Binding #inputDouble.Value}"></TextBlock>
<u:NumericByteUpDown Name="inputByte" Step="1" Value="3" EmptyInputValue="1"></u:NumericByteUpDown>
<TextBlock Text="{Binding #inputByte.Value}"></TextBlock>
<TextBlock Text="Drag"></TextBlock>
<u:NumericIntUpDown Step="1" Value="2" Watermark="Input Value" IsTextEditable="False" />
<u:NumericIntUpDown Step="1" Value="2" Watermark="Input Value" AllowDrag="True" />
</StackPanel>
</UserControl>

View File

@@ -38,8 +38,7 @@
VerticalAlignment="Stretch"
Background="Transparent"
Cursor="SizeAll"
IsVisible="{TemplateBinding IsTextEditable,
Converter={x:Static BoolConverters.Not}}" />
IsVisible="{TemplateBinding AllowDrag}" />
</Panel>
</ButtonSpinner>
</DataValidationErrors>

View File

@@ -27,13 +27,13 @@ public abstract class NumericUpDown : TemplatedControl
private Point? _point;
protected internal bool _updateFromTextInput;
public static readonly StyledProperty<bool> IsTextEditableProperty = AvaloniaProperty.Register<NumericUpDown, bool>(
nameof(IsTextEditable), defaultValue: true);
public static readonly StyledProperty<bool> AllowDragProperty = AvaloniaProperty.Register<NumericUpDown, bool>(
nameof(AllowDrag), defaultValue: false);
public bool IsTextEditable
public bool AllowDrag
{
get => GetValue(IsTextEditableProperty);
set => SetValue(IsTextEditableProperty, value);
get => GetValue(AllowDragProperty);
set => SetValue(AllowDragProperty, value);
}
public static readonly StyledProperty<bool> IsReadOnlyProperty = AvaloniaProperty.Register<NumericUpDown, bool>(
@@ -183,6 +183,10 @@ public abstract class NumericUpDown : TemplatedControl
{
CommitInput(true);
base.OnLostFocus(e);
if(AllowDrag && _dragPanel is not null)
{
_dragPanel.IsVisible = true;
}
}
protected override void OnKeyDown(KeyEventArgs e)
@@ -192,11 +196,23 @@ public abstract class NumericUpDown : TemplatedControl
var commitSuccess = CommitInput(true);
e.Handled = !commitSuccess;
}
if (e.Key == Key.Escape)
{
if (AllowDrag && _dragPanel is not null)
{
_dragPanel.IsVisible = true;
}
}
}
private void OnDragPanelPointerPressed(object sender, PointerPressedEventArgs e)
{
_point = e.GetPosition(this);
if (e.ClickCount == 2 && _dragPanel is not null && AllowDrag)
{
_dragPanel.IsVisible = false;
}
_textBox?.Focus();
}
@@ -207,7 +223,7 @@ public abstract class NumericUpDown : TemplatedControl
private void OnDragPanelPointerMoved(object sender, PointerEventArgs e)
{
if (IsTextEditable) return;
if (!AllowDrag) return;
if(!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) return;
var point = e.GetPosition(this);
var delta = point - _point;
@@ -420,6 +436,7 @@ public abstract class NumericUpDownBase<T>: NumericUpDown where T: struct, IComp
{
_textBox.Text = ConvertValueToText(Value);
}
SetValidSpinDirection();
}
protected virtual T? Clamp(T? value, T max, T min)