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.
Related
I need to set focus to UserControl itself, not its child.
Otherwise I cannot implement insertion from the buffer. :(
Setting Focusable=True doesn't help.
Google and SO tells only how to set focus to UserControl child.
My control contains:
- Toolbar with several buttons bound to commands of the corresponding
VM
- TextBox which is the input for the filter
- DataGrid - list of items.
I need to bind Ctrl+V command to VM. But to handle this gesture UserControl must have focus within. When there are no items in the grid (VM's collection is empty) buttons are disabled and the only element which can get focus is TextBox. But it handles Ctrl+V in its own way and I don't want to change this behavior.
Thus, I need something to set focus to when I click the area of UserControl.
I believe UserControl is the best candidate for it.
But I don't know how to make it selectable.
The whole problem was in my misunderstanding of controls' behavior.
This SO question clearly shows it I believe.
Thus, setting UserControl.Focusable = true is not sufficient. To make it navigatable via keyboard IsTabStop must be true also. And to make UC selectable by mouse click we should call Focus() in mouse eventhandler. That's it.
Title pretty much explains it all really. I need to know how to automatically jump to the next element (the element that would get focus if the user presses the "Tab" key), whatever that may be, in the KeyDown event of a Silverlight TextBox. My TextBoxes are generated dynamically so I can't, for example, manually code TextBox1 to set focus on TextBox2 on keydown etc. Any help would be greatly appreciated
I don't believe there is an easy way to accomplish this. The only method I know of is to use the VisualTreeHelper and get all the elements and then loop through them by their tab stop values.
If your textboxes are generated dynamically you could use a Behavior to set their tab stop properties pretty easily which would be a much better solution. Or just set it when you create them.
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
I'm looking to create a WPF textbox control that acts similar to the email recipient textboxes in Outlook (the To, Cc and Bcc inputs). I don't necessarily care that much about auto-completion (i've found millions of examples for that), but what I'm really struggling with is how to have delimited text entries in the textbox behave as entities the way they do in Outlook (once a recipient you've entered resolves, that text becomes an 'entity' which you can click to select, right click to get a context menu, etc. it's not longer 'plain text' in which you can place your cursor)...
Does anyone have any high level ideas how to accomplish this? Know of any existing examples (I've googled for hours)?
Thanks so much in advance,
Michael.
My rough thought process would be this... (note: I'm not actually coding it, so my details may be a little off...).
High level behaviour:
the type of data in your control is a list of items which aren't selectable. Therefore your control is, approximately, an ItemsControl (in terms of visual/XAML, it's an ItemsControl with a WrapPanel style layout and very simple TextBlock for the item template).
when your control gains focus, you need to switch the template to be a TextBox
when your control loses focus, you need to split the inputted text and convert it to a list for display.
Therefore, thinking code:
you need a UserControl, possibly derived from ItemsControl. That gives you basic behaviour to represent a list of items.
you need a custom DependencyProperty on your control that represents the delimited string.
when the string property changes, you need to parse it and replace the list of items in the control.
when the list property changes, you need to replace the string property with a suitably-delimited list.
In terms of code-behind, that part should be pretty simple. Then, for the XAML template...
you need a base template that displays your Items property as a list, using the WrapPanel layout mentioned above.
you need a trigger that replaces this template when the control has focus.
the replacement template should be a TextBox that is bound to the string property of the control.
the default binding behaviour on a TextBox will only push a new value when the TextBox loses focus, so you need to think about whether you want to make, say, an "Enter" keypress move focus (thus reverting the template to the list version - when the string property's value changes, your codebehind will update the list).
This should give you the basic behaviour. You should be able to bind either the list property or the string property from outside of the control, though you may have to be careful about what happens if you bind both properties since there's a two-way dependency between them...
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.