feat: Add Clear.

This commit is contained in:
rabbitism
2024-01-15 00:07:09 +08:00
parent 296777febb
commit 8230c8ec24
3 changed files with 46 additions and 1 deletions

View File

@@ -14,7 +14,7 @@
</Style> </Style>
</UserControl.Styles> </UserControl.Styles>
<StackPanel HorizontalAlignment="Left"> <StackPanel HorizontalAlignment="Left">
<u:NumericIntUpDown Name="input" Step="1" Value="2" Watermark="Input Value" IsReadOnly="True" /> <u:NumericIntUpDown Name="input" Step="1" Value="2" Watermark="Input Value" Classes="ClearButton" />
<TextBlock Text="{Binding #input.Value}" ></TextBlock> <TextBlock Text="{Binding #input.Value}" ></TextBlock>
<u:NumericDoubleUpDown Name="inputDouble" Step="0.5" Value="3.1" EmptyInputValue="1"></u:NumericDoubleUpDown> <u:NumericDoubleUpDown Name="inputDouble" Step="0.5" Value="3.1" EmptyInputValue="1"></u:NumericDoubleUpDown>
<TextBlock Text="{Binding #inputDouble.Value}"></TextBlock> <TextBlock Text="{Binding #inputDouble.Value}"></TextBlock>

View File

@@ -3,6 +3,27 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:u="https://irihi.tech/ursa"> xmlns:u="https://irihi.tech/ursa">
<!-- Add Resources Here --> <!-- Add Resources Here -->
<ControlTheme x:Key="InputClearButton" TargetType="Button">
<Setter Property="Button.Foreground" Value="{DynamicResource TextBoxButtonDefaultForeground}" />
<Setter Property="Button.Cursor" Value="Hand" />
<Setter Property="Button.Template">
<ControlTemplate TargetType="Button">
<!-- Background must be transparent or hit test will fail -->
<ContentControl Background="Transparent">
<PathIcon
Width="16"
Height="16"
Data="{DynamicResource TextBoxClearButtonData}"
Foreground="{TemplateBinding Foreground}" />
</ContentControl>
</ControlTemplate>
</Setter>
<Style Selector="^:pointerover">
<Setter Property="Foreground" Value="{DynamicResource TextBoxButtonPointeroverForeground}" />
</Style>
</ControlTheme>
<ControlTheme x:Key="{x:Type u:NumericUpDown}" TargetType="{x:Type u:NumericUpDown}"> <ControlTheme x:Key="{x:Type u:NumericUpDown}" TargetType="{x:Type u:NumericUpDown}">
<Setter Property="NumericUpDown.VerticalContentAlignment" Value="Center" /> <Setter Property="NumericUpDown.VerticalContentAlignment" Value="Center" />
<Setter Property="NumericUpDown.CornerRadius" Value="{DynamicResource NumericUpDownCornerRadius}" /> <Setter Property="NumericUpDown.CornerRadius" Value="{DynamicResource NumericUpDownCornerRadius}" />
@@ -39,10 +60,27 @@
Background="Transparent" Background="Transparent"
Cursor="SizeAll" Cursor="SizeAll"
IsVisible="{TemplateBinding AllowDrag}" /> IsVisible="{TemplateBinding AllowDrag}" />
<Button
Name="PART_ClearButton"
Command="{Binding $parent[u:NumericUpDown].Clear}"
HorizontalAlignment="Right"
Margin="0 0 8 0"
IsVisible="False"
Focusable="False"
Theme="{StaticResource InputClearButton}" />
</Panel> </Panel>
</ButtonSpinner> </ButtonSpinner>
</DataValidationErrors> </DataValidationErrors>
</ControlTemplate> </ControlTemplate>
</Setter> </Setter>
<Style Selector="^.clearButton, ^.ClearButton">
<Style Selector="^[IsReadOnly=False]:focus /template/ Button#PART_ClearButton">
<Setter Property="IsVisible" Value="True" />
</Style>
<Style Selector="^[IsReadOnly=False]:pointerover /template/ Button#PART_ClearButton">
<Setter Property="IsVisible" Value="True" />
</Style>
</Style>
</ControlTheme> </ControlTheme>
</ResourceDictionary> </ResourceDictionary>

View File

@@ -290,6 +290,8 @@ public abstract class NumericUpDown : TemplatedControl
protected abstract bool SyncTextAndValue(bool fromTextToValue = false, string? text = null, protected abstract bool SyncTextAndValue(bool fromTextToValue = false, string? text = null,
bool forceTextUpdate = false); bool forceTextUpdate = false);
public abstract void Clear();
} }
public abstract class NumericUpDownBase<T>: NumericUpDown where T: struct, IComparable<T> public abstract class NumericUpDownBase<T>: NumericUpDown where T: struct, IComparable<T>
@@ -613,4 +615,9 @@ public abstract class NumericUpDownBase<T>: NumericUpDown where T: struct, IComp
protected abstract T? Add(T? a, T? b); protected abstract T? Add(T? a, T? b);
protected abstract T? Minus(T? a, T? b); protected abstract T? Minus(T? a, T? b);
public override void Clear()
{
SetCurrentValue(ValueProperty, EmptyInputValue);
SyncTextAndValue(false, forceTextUpdate: true);
}
} }