Skip to content

Commit

Permalink
Merge pull request #495 from irihitech/numeric
Browse files Browse the repository at this point in the history
Fix numeric family focus issue.
  • Loading branch information
rabbitism authored Nov 29, 2024
2 parents c2974a5 + d16b559 commit e22fa2a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
34 changes: 11 additions & 23 deletions demo/Sandbox/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,17 @@
</Design.DataContext>

<Grid>
<DataGrid ItemsSource="{Binding Items}" IsReadOnly="False" HorizontalAlignment="Left">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>

<DataGridTemplateColumn Width="2*" Header="Address" MaxWidth="300" MinWidth="300">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{Binding Address}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<u:IPv4Box
HorizontalAlignment="Stretch"
IPAddress="{Binding Address}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<u:Form>
<u:FormItem Label="_Numeric">
<u:NumericIntUpDown/>
</u:FormItem>
<u:FormItem Label="_AnotherNumeric">
<u:NumericIntUpDown/>
</u:FormItem>
<u:FormItem Label="_TextBox">
<TextBox/>
</u:FormItem>
</u:Form>
</Grid>

</Window>
30 changes: 30 additions & 0 deletions src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public abstract class NumericUpDown : TemplatedControl, IClearControl, IInnerCon
protected ButtonSpinner? _spinner;
protected TextBox? _textBox;
protected internal Panel? _dragPanel;
private bool _isFocused;

private Point? _point;
protected internal bool _updateFromTextInput;
Expand Down Expand Up @@ -149,6 +150,7 @@ public bool ShowButtonSpinner

static NumericUpDown()
{
FocusableProperty.OverrideDefaultValue<NumericUpDown>(true);
NumberFormatProperty.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));
Expand Down Expand Up @@ -216,6 +218,34 @@ protected override void OnLostFocus(RoutedEventArgs e)
{
_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();
}
}
}

protected override void OnKeyDown(KeyEventArgs e)
Expand Down

0 comments on commit e22fa2a

Please sign in to comment.