How to set the focus on a user control item in WPF? - wpf

I have a WPF app that uses different instances of a User Control; when the user clicks a button on the main window I need to set the focus on the textbox inside the currently active instance of the user control.
Which is the best way to set the focus on that child item?

child.Focus();
or
VisualTreeHelper.GetChild(parent, 0).Focus(); // you might want to test it for nulls

Related

Can we tab on TabIndex only without TabStop

I have an UI who have a lot of object. I want my tab to circle between some object.
With
KeyboardNavigation.TabIndex="0"
But i don't want to stop on some object without TabIndex.
I know i can use
TabStop="False"
But i have a LOT of objects with children who are user control... Is their a way to say to WPF to only tab stop on item who have a tabindex for a user control?
Is their a way to say to WPF to only tab stop on item who have a tabindex for a user control?
No, because KeyboardNavigation.TabIndex is an attached property that has a default value of Int32.MaxValue unless you set it to something else for a specific element.
So there is no "only items who have a tabindex". You'll have to set the TabStop property to false for all items that you don't want to receive focus using the TAB key.

How to set focus to a default child control unless the user clicks on a different child control?

I have a pretty simple use case but I cannot make it work right. I've got a ListView whose item template is a Note custom UserControl:
Each Note has a few simple controls as shown.
I want this to work so that if a row is selected programmatically or by clicking somewhere in it, it sets focus to the first textbox in that row. But if you click on a control in the row, it activates that control (i.e. lets you edit the Exhibit contents or click the Delete button).
If I don't do anything to set focus, clicking on the row highlights the row but doesn't set focus to any child control.
Within the Note control I have tried this:
protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
base.OnGotKeyboardFocus(e);
text.Focus();
}
text is the name of that first TextBox, and it does set focus, but it also does this if I click directly on the Exhibit textbox or Delete button, making them unusable.
So, how can I enable the focusing that I want when the container control gets focus, UNLESS it got that focus via a click on a specific child control (which should then keep the focus)?
After a bit more searching, I found the correct way to implement this: at the root of the container control (the Note UserControl in my case), you can specify the name of the control to be the default focus control:
FocusManager.FocusedElement="{Binding ElementName=text}
Not sure how I missed that one.

Expand Usercontrol to full window size

I am currently working on a project which has a tab control which contains a Wrap panel which contain a series of user controls. I am looking for a way to allow the user to select one user control and maximize it to the size of the tab control/window.
One thought is to simply remove all the other items from the panel.However I am attempting to use MVVM as much as possible and I'm not sure how much the user control should know about the panel. (The user control will contain a button to allow maximizing)
Is there a way to temporarily remove the usercontrol from the grid and treat it like a modal popup or just to fill the window?
How about having "Visible" or "Maximized" bool properties in the view model for each user control based item, and databind said user controls Visibility property to the appropriate property. Then bind your user controls maximize/restore button to command in the view model to change the VM properties appropriately?

Navigating a WPF Tab Control from within a User Control?

My WPF application consists of a main window with a tab control which has a series of tab items, each hosting a user control.
I'd like one of the user controls to be able to trigger the application to change focus from the current tab to a different one.
Is there a way for the user control to trigger its tab control container to change to another tab item?
The WPF system provides the RoutedEvent. This special kind of event can be created to be catched by every element in the tree. With this way you can fire the event inside your user control, and catch it in the TabControl that will do everything you need. The tab control can catch the event cause of it lies in the element's tree of your window.
You can start from here:
http://msdn.microsoft.com/en-us/library/ms742806.aspx
You'll need a Bubble Event.
Hope this helps.
You can have a property that binds with SelectedItem property of TabControl.

Disable TAB access to control in WinForms

Can I somehow disable access by TAB on come controls on form in WinForms
(controls like textboxes must be enabled for access and writing but when user hits TAB it will access only buttons)
Set the TabStop property of the control to false.
You can set TabStop=false for individual controls.
Find the Tab Stop property on the the Other tab of the property sheet and set it to No for each TextBox you want to prevent the users from tabbing to.

Resources