Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding a UserControl to a custom BusyIndicator control
    primarykey
    data
    text
    <p>I have a requirement to focus on a specific textbox when a new view is loaded.</p> <p>The solution was to add this line of code to the OnLoaded event for the view:</p> <pre><code>Dispatcher.BeginInvoke(() =&gt; { NameTextBox.Focus(); }); </code></pre> <p>So this worked for one view, but not another. I spent some time debugging the problem and realized that the new view I was working on had a BusyIndicator that takes focus away from all controls since the BusyIndicator being set to true and false was occuring after the OnLoaded event.</p> <p>So the solution is to call focus to the <code>NameTextBox</code> <strong>after</strong> my BusyIndicator has been set to false. My idea was to create a reusable BusyIndicator control that handles this extra work. However, I am having trouble doing this in MVVM.</p> <p>I started by making a simple extension of the toolkit:BusyIndicator:</p> <pre><code>public class EnhancedBusyIndicator : BusyIndicator { public UserControl ControlToFocusOn { get; set; } private bool _remoteFocusIsEnabled = false; public bool RemoteFocusIsEnabled { get { return _remoteFocusIsEnabled; } set { if (value == true) EnableRemoteFocus(); } } private void EnableRemoteFocus() { if (ControlToFocusOn.IsNotNull()) Dispatcher.BeginInvoke(() =&gt; { ControlToFocusOn.Focus(); }); else throw new InvalidOperationException("ControlToFocusOn has not been set."); } </code></pre> <p>I added the control to my XAML file with no problem:</p> <pre><code>&lt;my:EnhancedBusyIndicator ControlToFocusOn="{Binding ElementName=NameTextBox}" RemoteFocusIsEnabled="{Binding IsRemoteFocusEnabled}" IsBusy="{Binding IsDetailsBusyIndicatorActive}" ... &gt; ... &lt;my:myTextBox (this extends TextBox) x:Name="NameTextBox" ... /&gt; ... &lt;/my:EnhancedBusyIndicator&gt; </code></pre> <p>So the idea is when <code>IsRemoteFocusEnabled</code> is set to true in my ViewModel (which I do after I've set <code>IsBusy</code> to false in the ViewModel), focus will be set to <code>NameTextBox</code>. And if it works, others could use the <code>EnhancedBusyIndicator</code> and just bind to a different control and enable the focus appropriately in their own ViewModels, assuming their views have an intial <code>BusyIndicator</code> active.</p> <p>However, I get this exception when the view is loaded:</p> <p><strong>Set property 'foo.Controls.EnhancedBusyIndicator.ControlToFocusOn' threw an exception. [Line: 45 Position: 26]</strong></p> <p>Will this solution I am attempting work? If so, what is wrong with what I have thus far (cannot set the <code>ControlToFocusOn</code> property)?</p> <hr> <p><strong>Update 1</strong></p> <p>I installed Visual Studio 10 Tools for Silverlight 5 and got a better error message when navigating to the new view. Now I gete this error message:</p> <p>"System.ArgumentException: Object of type System.Windows.Data.Binding cannot be converted to type System.Windows.Controls.UserControl"</p> <p>Also, I think I need to change the DataContext for this control. In the code-behind constructor, DataContext is set to my ViewModel. I tried adding a DataContext property to the <code>EnhancedBusyIndicator</code>, but that did not work:</p> <pre><code>&lt;my:EnhancedBusyIndicator DataContext="{Binding RelativeSource={RelativeSource Self}}" ControlToFocusOn="{Binding ElementName=NameTextBox}" RemoteFocusIsEnabled="{Binding IsRemoteFocusEnabled}" IsBusy="{Binding IsDetailsBusyIndicatorActive}" ... &gt; </code></pre> <hr> <p><strong>Update 2</strong></p> <p>I need to change <code>UserControl</code> to <code>Control</code> since I will be wanting to set focus to <code>TextBox</code> objects (which implement <code>Control</code>). However, this does not solve the issue.</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.
 

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