How to disable "select all" in a WPF TextBox - wpf

On mouseclick on a WPF TextBox, the textbox gets the focus and selects all the text.
How I can this disable this behavior? On click only the caret shall be positioned on the char (as in good old windows).
Why? If I click on a textbox i want to mark some text, or set the carret position, to type more text. I do not want to deselect the whole marked text first and then click on the position again.
Another thing is, when the window lost the focus and get it back, the whole text is selected again and the carret position is lost. So it is not possible to copy text from another window and paste to the textbox w/o to select the correct carret position again.
Maybe this is a system "feature" (W8.1/W10) because non WPF textboxes have the same behavior, but this is annoying. Thanks for your helping ideas.

My text box does not do this by default, The only one thing what i have done with it just bind TextBox.Text property with my ViewModel's property
Here described the mechanism of things that you have mentioned above, you just need to revert them
https://social.msdn.microsoft.com/Forums/vstudio/en-US/564b5731-af8a-49bf-b297-6d179615819f/how-to-selectall-in-textbox-when-textbox-gets-focus-by-mouse-click?forum=wpf

Related

How do I pick right TextBox to input value in it

Need help in achieving functionality for the image that I insert.
I have 4-5 textboxes and near them some type of calculator.
All TextBoxes are bound to properties in ViewModel.
All calculator buttons use Command="{Binding AddNumberCommand}" CommandParameter="9" for passing value to a property.
Problem is that I don't know how to pick the right TextBox for inputting value.
Thanks for the advice.
There are a couple ways I could think of to do this. Note that whichever way you do it, I'm pretty sure you'll have to set Focusable to false on all your Buttons, otherwise they'll steal the focus away from the TextBoxes when clicked.
Keyboard Focus Events
You could declare a field where you track the currently focused TextBox (e.g. private TextBox focusedBox;). And for each TextBox, you could handle the GotKeyboardFocus and LostKeyboardFocus events. You could use those events to track which TextBox currently has focus, and when you want to add a number you just add it to focusedBox.
You would also have to check if focusedBox is null, in case none of your TextBoxes are currently focused.
This method would ensure that the calculator buttons only ever effect those TextBoxes you want them to.
Keyboard.FocusedElement
Alternatively, you could just use the Keyboard.FocusedElement static property. This will point to whatever element currently has keyboard focus. This coud be any focusable element in your application, or null if the keyboard focus is outside your application.
You could attempt to cast Keyboard.FocusedElement to TextBox, and if the cast succeeds, append the text.
This would require much less code, but also allows the buttons to add text to any TextBox in your application. This may or may not be a problem, and could be prevented by setting some unique property on the TextBoxes you want to allow input on and checking for it, or keeping a list of said TextBoxes.

wpf selecting controls inside a listbox

I have a textbox and some labels inside the data template of bounded listbox.
When I click on any label the whole item is highlighted in blue, but when I click directly on a different textbox the selection does not change.
Is there a way to make the selection of the listbox change even when a textbox is clicked?
thanks
This is what I've exactly asked few days ago, see post: "WPF: Trigger SelectedIndex changed whilst clicking on any control within a ListBoxItem area"
basically there are few solutions, using code behind and XAML, but I've not verified latter approach yet
The reason is because the TextBox handles the click event in order to receive focus. There are a number of ways to handle this, including but not limited to:
stop the TextBox handling mouse events (which prevents the user from focussing it using the mouse)
use an eventhandler when the TextBox gains focus (or PreviewClick or similar), to select the parent ListItem

Controlling the position of Silverlight textbox's caretbrush

I believe the cursor over a textbox is called a caretbrush.
There are times application is run without a keyboard/ mouse (eg: Kiosk), so I will put up a virtual keyboard for the user.
The virtual keyboard appends to the textbox programmatically.
When I focus on the textbox, irregardless of have any text in it, the caretbrush will always be at the leftmost position.
Is there anyway I can control the position of it?
If unable to do it, guess I just hide the caret brush, otherwise it looks abit confusing especially when u are at the 3rd character but the caretbrush shows on the 1st character.
I found the answer already.
txt1.SelectionStart=txt1.text.length
but txt1.SelectionStart=txt1.text.length-1 is only correct

How to 'get at' the WPF combobox PART_EditableTextbox because combobox not getting highlighted?

My WPF combobox is populated with a different set of strings each click of a button. There are other controls on the window as well. The combobox is the 'first' (top) in the window, but the text doesn't get highlighted. When the user tabs through the controls, the text DOES get highlighted, but when it's the first on the window, it doesn't.
Maybe i need to force a highlight on the individual textbox control 'within' the combobox itself, but how would i do this? I couldnt seem to find the internal 'structure' of this control anywhere. Could anyone help here?
Jack
to get the TextBox of Combobox you can use
TextBox TxtBox = (TextBox)myCombo.Template.FindName("PART_EditableTextBox", myCombo);
I'm not sure it's the best solution, but you can use FrameworkElement.FindName to access the child control -- it's guaranteed to be present in a combobox, because it's a key constituent part of the control.
That stated, is it not better to try and call .Focus() on the control? That is likely why when you tab, the highlight is provided.
Another option is to derive from ComboBox, and expose the child text box as a property allowing you to set it's selection, or add a method directly to the combobox to set it for you.

WPF Datagrid deselects row when control is disabled

I have a program in which a user selects a row in a Datagrid and then clicks a "Start Recording" button. While "recording" is happening, they are not allowed to change the value selected in the datagrid, so I set IsEnabled to false. However, when the datagrid is set to be disabled, it deselects the selected row, which screws up any bindings I have to the datagrid's SelectedItem propery.
Is there any way to keep the datagrid row selected even though the control is disabled?
Edit: This does not happen in Windows Vista, but it does in Windows 7.
If you really want to 'record' actions but still keep visuals and interactions looking the same, why don't you just add a check to the event fired on selection to ensure that recording is not taking place and set e.Handled = true.
Alternatively you could set IsHitTestVisible = false and prevent them from taking actions in the control instead of disabling it outright.
Hope that helps.
Sorry I know this post is a little old, but I couldn't find another solution to this anywhere else.
It doesn't seem to be related to Vista\7, but to the Feb. release of the Toolkit.
You can set IsHitTestVisible = false as Jeff Wain suggests, but as Mike noted it doesn't appear different. Also, It doesn't disable Keyboard input.
My solution is to put the DataGrid in a Grid within the same row and column as a semitransparent gray rectangle (This will make them on top of one another). You have to put the rectangle in the Grid second to make sure it is on top of the DataGrid. When I want to 'disable' it I make the rectangle visible. This will make the list look dimmed and disable mouse inputs, but it still doesn't disable keyboard input.
To disable the keyboard I have to intercept 'PreviewKeyDown' and set e.Handdled = true. This will not allow anything else to be selected but will still do some interesting things when you tab to it (like tab no longer working). Perhaps setting it to not be a tab stop and not focusable will also fix this, but disabling selection is all I really care about.
IsHitTestVisible=false disables mouse inputs.
For disabling keyboard inputs set Focusable=false.
Both should be set via a style in ElementStyle and/or ElementEditingStyle for builtin datagrid columns inorder for the child control (textbox, checkbox, etc) to not accept input.
You'll most likely have to use a trigger in the style and bind it to some IsRecording value.
Also you could, in the same style, change the appearnce of the "disabled" controls by setting their Opacity=0.4, this gives somewhat of a disabled feel to them.

Resources