0

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" >
NetUser101
  • 272
  • 2
  • 5
  • 20
  • http://doc.xceedsoft.com/products/XceedWpfToolkit/Xceed.Wpf.Toolkit~Xceed.Wpf.Toolkit.Primitives.UpDownBase%601~TextBox.html shows that perhaps you want to focus on the Textbox within the control not the control itself. Becase it's a Textbox if you want to highlight to text, you can use the SelectAll method of the Textbox itself. – JWP Jan 20 '16 at 10:43
  • I agree with you on the fact that I think that this is also the right way to do this. Unfortunately I tried to access the TextBox from DecimalUpDown and it won't let me do so. – NetUser101 Jan 20 '16 at 12:43
  • You could derive your own class from `DecimalUpDown` so you can have access to `protected TextBox`, and then, whenever convenient, call `TextBox.Focus()`, for instance, on `Loaded` event; – jsanalytics Jan 22 '16 at 00:37

1 Answers1

0

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.

Logan
  • 806
  • 8
  • 17
  • 1
    I have tried this but the DecimalUpDown does not have a SelectAll Method. – NetUser101 Jan 20 '16 at 12:42
  • Oh. That's... weird. It is possible to select characters inside it though, right? It has to have something like SelectAll. (Sorry the answer didn't work). – Logan Jan 20 '16 at 16:28
  • Yes the issue is that I cannot programmatically select the entire value inside of it, however it does select it if I actually click on it with the mouse. – NetUser101 Jan 22 '16 at 09:52