mirror of
https://github.com/yaobiao131/downkyicore.git
synced 2025-08-10 00:52:31 +00:00
49
DownKyi/CustomAction/NumericInputBehavior.cs
Normal file
49
DownKyi/CustomAction/NumericInputBehavior.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System.Linq;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Xaml.Interactivity;
|
||||
|
||||
namespace DownKyi.CustomAction;
|
||||
|
||||
public class NumericInputBehavior : Behavior<TextBox>
|
||||
{
|
||||
protected override void OnAttached()
|
||||
{
|
||||
if (AssociatedObject != null)
|
||||
{
|
||||
AssociatedObject.AddHandler(InputElement.TextInputEvent, OnTextInput, RoutingStrategies.Tunnel);
|
||||
AssociatedObject.TextChanging += OnTextChanging;
|
||||
}
|
||||
base.OnAttached();
|
||||
}
|
||||
|
||||
protected override void OnDetaching()
|
||||
{
|
||||
if (AssociatedObject != null)
|
||||
{
|
||||
AssociatedObject.RemoveHandler(InputElement.TextInputEvent, OnTextInput);
|
||||
AssociatedObject.TextChanging -= OnTextChanging;
|
||||
}
|
||||
base.OnDetaching();
|
||||
}
|
||||
|
||||
private void OnTextInput(object? sender, TextInputEventArgs e)
|
||||
{
|
||||
if (string.IsNullOrEmpty(e.Text) || e.Text.All(char.IsControl))
|
||||
return;
|
||||
|
||||
if (!char.IsDigit(e.Text[0]))
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
private void OnTextChanging(object? sender, TextChangingEventArgs e)
|
||||
{
|
||||
if (e.Source is TextBox t &&
|
||||
!string.IsNullOrEmpty(t.Text) && !t.Text.All(char.IsDigit))
|
||||
{
|
||||
t.Text = new string(t.Text.Where(char.IsDigit).ToArray());
|
||||
t.CaretIndex = t.Text?.Length ?? 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
x:Class="DownKyi.CustomControl.CustomPager"
|
||||
xmlns:vmc="clr-namespace:DownKyi.CustomControl"
|
||||
x:DataType="vmc:CustomPagerViewModel"
|
||||
xmlns:action="clr-namespace:DownKyi.CustomAction"
|
||||
x:CompileBindings="False">
|
||||
|
||||
<UserControl.Resources>
|
||||
@@ -234,5 +235,35 @@
|
||||
VerticalAlignment="Center"
|
||||
Source="{StaticResource ToRightDrawingImage}" />
|
||||
</Button>
|
||||
|
||||
<Panel Margin="20,0,0,0">
|
||||
<StackPanel Orientation="Horizontal" >
|
||||
<TextBox
|
||||
x:Name="JumpPageNumberTextBox"
|
||||
Margin="5,0,0,0"
|
||||
VerticalAlignment="Center"
|
||||
MaxLength="5"
|
||||
Watermark="{DynamicResource JumpTo}"
|
||||
CornerRadius="4"
|
||||
Width="65">
|
||||
<Interaction.Behaviors>
|
||||
<ExecuteCommandOnKeyDownBehavior
|
||||
Key="Enter"
|
||||
CommandParameter="{Binding #JumpPageNumberTextBox.Text}"
|
||||
Command="{Binding JumpCommand}" />
|
||||
<action:NumericInputBehavior/>
|
||||
</Interaction.Behaviors>
|
||||
</TextBox>
|
||||
|
||||
<TextBlock
|
||||
Margin="5,0,0,0"
|
||||
VerticalAlignment="Center"
|
||||
Foreground="#666">
|
||||
<Run>/</Run>
|
||||
<Run Text="{Binding Count}" ></Run>
|
||||
<Run Text="{DynamicResource Page}"></Run>
|
||||
</TextBlock>
|
||||
</StackPanel>
|
||||
</Panel>
|
||||
</StackPanel>
|
||||
</UserControl>
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using Avalonia.Controls;
|
||||
using Prism.Commands;
|
||||
|
||||
namespace DownKyi.CustomControl;
|
||||
@@ -77,6 +78,9 @@ public class CustomPagerViewModel : INotifyPropertyChanged
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private int _current;
|
||||
|
||||
@@ -385,7 +389,20 @@ public class CustomPagerViewModel : INotifyPropertyChanged
|
||||
|
||||
SetView();
|
||||
}
|
||||
|
||||
private DelegateCommand<object>? _jumpCommand;
|
||||
|
||||
public DelegateCommand<object> JumpCommand => _jumpCommand ??= new DelegateCommand<object>(JumpExecuted);
|
||||
|
||||
private void JumpExecuted(object obj)
|
||||
{
|
||||
if (obj is string s && int.TryParse(s,out var i))
|
||||
{
|
||||
Current = (i >= _count) ? _count : i;
|
||||
SetView();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 控制显示,暴力实现,以后重构
|
||||
/// </summary>
|
||||
|
||||
@@ -352,5 +352,9 @@
|
||||
<system:String x:Key="Yes">是</system:String>
|
||||
<system:String x:Key="No">否</system:String>
|
||||
<system:String x:Key="ApplyToAll">全部应用</system:String>
|
||||
|
||||
|
||||
|
||||
<!-- CustomControl -->
|
||||
<system:String x:Key="Page">页</system:String>
|
||||
<system:String x:Key="JumpTo">跳至</system:String>
|
||||
</ResourceDictionary>
|
||||
Reference in New Issue
Block a user