Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/questions/980120/finding-control-within-wpf-itemscontrol">This SO article</a> describes how you can select the container of an item in an ItemsControl and navigate the tree to select the item you want to alter (in this case focusing it).</p> <p>Revising my code from below:</p> <pre><code>public void WhateverMethodForShowingBusy () { //Get a reference to your View FrameworkElement myView = this.View; // I generally have a reference to the view living on the ViewModel set at construction time // Get a reference to your ItemsControl - in this example by name ListBox custListBox = myView.ListBoxName; // Get the currently selected Item which will be a CustomerViewModel // (not a DataTemplate or ListBoxItem) CustomerViewModel cvm = custListBox.SelectedItem; //Generate the ContentPresenter ContentPresenter cp = custListBox.ItemContainerGenerator.ContainerFromItem(cvm) as ContentPresenter; //Now get the button and focus it. Button myButton = cp.FindName("MyButtonName"); myButton.Focus(); } </code></pre> <h2>The information below is incorrect based on the mistake that IsFocused was a read/write property that would set focus. It is not applicable.</h2> <p>This is another place where MVVM really works well. If you are unfamiliar with MVVM I would highly suggest looking into it. It solves a lot of problems like this and if implemented properly it can make your code much more maintainable.</p> <p>If you are using an MVVM approach, just host a boolean property (we'll call it IsFocused) on the ViewModel that is sitting behind the DataTemplate. For example we have a Customer class, a CustomerViewModel class which contains an instance of customer, and then your MainViewModel which contains a collection of CustomerViewModels. Your ItemsControl's ItemsSource property is bound to the CustomerViewModels collection, and the DataTemplate button's IsFocused property is bound to the IsFocused property on the CustomerViewModel.</p> <p>I am unsure of your workflow, but you can essentially do something like so:</p> <pre><code>public void WhateverMethodForShowingBusy () { //Get a reference to your View FrameworkElement myView = this.View; // I generally have a reference to the view living on the ViewModel set at construction time // Get a reference to your ItemsControl - in this example by name ListBox custListBox = myView.ListBoxName; // Get the currently selected Item which will be a CustomerViewModel // (not a DataTemplate or ListBoxItem) CustomerViewModel cvm = custListBox.SelectedItem; //Finally set the property. cvm.IsFocused = true; } </code></pre> <p>As with all things in MVVM, be sure that you are implementing INotifyPropertyChanged.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload