Search code examples
c#wpfxamlwpftoolkitwpf-extended-toolkit

WPF Extended ToolKit - Selecting value in DecimalUpDown


After hours of trying to solve this, I'm giving up and am actually wondering if there's a solution for this.

I have a WPF View that is setting the focus on a decimal up down from the code behind (xaml.cs). My aim is to select the value inside it since it loads "0.00" and it's frustrating for the user to have to delete it before he enters his value. I tried the following code in the loaded method:

    private void Loaded_Window(object sender, EventArgs e)
    {
        txtAmount.AutoSelectBehavior = AutoSelectBehavior.OnFocus;

        txtAmount.Focus();
        Keyboard.Focus(txtAmount);
    }

I have also tried to set SelectAllOnGotFocus as true and it still did not work. Funnily enough, this selects the test if I put a debug point in the method so I'm thinking that it has to do with the loading of the User Control. Does anyone have any pointers of how to make this work?

The xaml decimalupdown:

<xctk:DecimalUpDown x:Name="txtAmount" d:LayoutOverrides="Height" Minimum="0" Maximum="99999999.99" ShowButtonSpinner="False" UpdateValueOnEnterKey="True" Text="{Binding Amount, UpdateSourceTrigger=PropertyChanged}" AllowSpin="False" FormatString="N2" >

Solution

  • Try the following:

    private void [NAME OF YOUR EVENT GOES HERE] (object sender, KeyboardFocusChangedEventArgs e)
            {
                if (e.KeyboardDevice.IsKeyDown(Key.Tab))
                {
                    SelectAll();
                }
            }
    

    The "SelectAll" is really the main thing here, you can ignore the if statement i believe.

    In the XAML you will have to add the property "GotFocus" to your control like so:

    <xctk:DecimalUpDown GotFocus="[NAME OF YOUR EVENT GOES HERE]" />
    

    So that whenever it gains focus, the first thing it does is select everything ready for editing.