fix: fix numeric focus issue.

This commit is contained in:
rabbitism
2024-11-29 21:30:50 +08:00
parent f359f737da
commit 5ae4992743
2 changed files with 42 additions and 23 deletions

View File

@@ -17,29 +17,17 @@
</Design.DataContext> </Design.DataContext>
<Grid> <Grid>
<DataGrid ItemsSource="{Binding Items}" IsReadOnly="False" HorizontalAlignment="Left"> <u:Form>
<DataGrid.Columns> <u:FormItem Label="_Numeric">
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/> <u:NumericIntUpDown/>
</u:FormItem>
<DataGridTemplateColumn Width="2*" Header="Address" MaxWidth="300" MinWidth="300"> <u:FormItem Label="_AnotherNumeric">
<DataGridTemplateColumn.CellTemplate> <u:NumericIntUpDown/>
<DataTemplate> </u:FormItem>
<TextBlock <u:FormItem Label="_TextBox">
HorizontalAlignment="Center" <TextBox/>
VerticalAlignment="Center" </u:FormItem>
Text="{Binding Address}" /> </u:Form>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<u:IPv4Box
HorizontalAlignment="Stretch"
IPAddress="{Binding Address}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid> </Grid>
</Window> </Window>

View File

@@ -26,6 +26,7 @@ public abstract class NumericUpDown : TemplatedControl, IClearControl, IInnerCon
protected ButtonSpinner? _spinner; protected ButtonSpinner? _spinner;
protected TextBox? _textBox; protected TextBox? _textBox;
protected internal Panel? _dragPanel; protected internal Panel? _dragPanel;
private bool _isFocused;
private Point? _point; private Point? _point;
protected internal bool _updateFromTextInput; protected internal bool _updateFromTextInput;
@@ -149,6 +150,7 @@ public abstract class NumericUpDown : TemplatedControl, IClearControl, IInnerCon
static NumericUpDown() static NumericUpDown()
{ {
FocusableProperty.OverrideDefaultValue<NumericUpDown>(true);
NumberFormatProperty.Changed.AddClassHandler<NumericUpDown>((o, e) => o.OnFormatChange(e)); NumberFormatProperty.Changed.AddClassHandler<NumericUpDown>((o, e) => o.OnFormatChange(e));
FormatStringProperty.Changed.AddClassHandler<NumericUpDown>((o, e) => o.OnFormatChange(e)); FormatStringProperty.Changed.AddClassHandler<NumericUpDown>((o, e) => o.OnFormatChange(e));
IsReadOnlyProperty.Changed.AddClassHandler<NumericUpDown, bool>((o, args) => o.OnIsReadOnlyChanged(args)); IsReadOnlyProperty.Changed.AddClassHandler<NumericUpDown, bool>((o, args) => o.OnIsReadOnlyChanged(args));
@@ -216,6 +218,35 @@ public abstract class NumericUpDown : TemplatedControl, IClearControl, IInnerCon
{ {
_dragPanel.IsVisible = true; _dragPanel.IsVisible = true;
} }
FocusChanged(IsKeyboardFocusWithin);
}
protected override void OnGotFocus(GotFocusEventArgs e)
{
base.OnGotFocus(e);
FocusChanged(IsKeyboardFocusWithin);
}
private void FocusChanged(bool hasFocus)
{
// The OnGotFocus & OnLostFocus are asynchronously and cannot
// reliably tell you that have the focus. All they do is let you
// know that the focus changed sometime in the past. To determine
// if you currently have the focus you need to do consult the
// FocusManager.
bool wasFocused = _isFocused;
_isFocused = hasFocus;
if (hasFocus)
{
if (!wasFocused && _textBox != null)
{
_textBox.Focus();
_textBox.SelectAll();
}
}
} }
protected override void OnKeyDown(KeyEventArgs e) protected override void OnKeyDown(KeyEventArgs e)