Unburdening the end user

There are many things that we can do to make the life of the end users easier. One simple example would be to set the focus in a form to the first field, so that users can start typing as soon as they load a View, without first needing to focus it manually.

We saw one way to do this using an Attached Property in Chapter 4, Becoming Proficient with Data Binding, but we can also achieve this easily, by first adding a new bool property into our BaseViewModel class:

private bool isFocused = false;

...

public bool IsFocused 
{ 
  get { return isFocused; } 
  set { if (isFocused != value) { isFocused = value;
    NotifyPropertyChanged(); } } 
}

Next we can add a style resource into the application resources in the App.xaml file:

<Style TargetType="{x:Type TextBox}"> 
  <!-- Define default TextBox style here --> 
</Style> 
<Style x:Key="FocusableTextBoxStyle" TargetType="{x:Type TextBox}"  
  BasedOn="{StaticResource {x:Type TextBox}}"> 
  <Style.Triggers> 
    <DataTrigger Binding="{Binding IsFocused}" Value="True"> 
      <Setter Property="FocusManager.FocusedElement"  
        Value="{Binding RelativeSource={RelativeSource Self}}" /> 
    </DataTrigger> 
  </Style.Triggers> 
</Style> 

This assumes that we already have a default style that we want to use for our TextBox controls and that our new style will be based on that, but add this additional focusable functionality. It simply consists of a single data trigger that uses the FocusedElement property of the FocusManager class to focus the TextBox element that has this style applied to it when the IsFocused property is set to true.

Therefore, all we need to do to focus a particular TextBox control in a View is to apply this style to it and set the IsFocused property from the BaseViewModel class to true in the appropriate place in the related View Model:

IsFocused = true; 

Note that the TextBox control will become focused as the property becomes true , and so if the property is already true, we may need to first set it to false before again setting it to true to get this to work. For example, if the property was true before the View was loaded, then the TextBox control would not become focused.

Another simple example of making our application users' lives easier would be to pre-populate any form fields that we may be able to. For example, if our application has a login screen that uses the users' Windows username, we could fill in the user name field in the form after accessing it from the WindowsIdentity class like this:

UserName = WindowsIdentity.GetCurrent().Name; 

Another example of this might be to pre-populate form fields with the most commonly used values. We could perhaps fill in a date field with today's date or an Amount Paid field to the total amount, if that is what the users typically do.

We do, however, need to be careful when doing this because if we get the default value(s) wrong, it could backfire and actually take the users longer to delete the default value and replace it with the value that they want than to just input the value directly. Remember, the idea is to save the users time and make them more productive.

Quite often, we can save the users of our applications a great amount of time. If we have the chance to ask them exactly what they do and how they would use the application on a day-to-day basis, then we can usually program a lot of their operations into functions in the application.

For example, if any users have to repeatedly edit a number of files with the same data, perhaps to add, remove, or update a particular field, then we could build that functionality straight into the application.

Instead of making them edit a single record at a time, we could provide a View where they set the field, or fields to change, and the new value(s), along with the ability to select multiple records, and therefore save them a great deal of time and effort.

All menial, or repetitive tasks can be programmed into functions, and so writing a good application is not just restricted to making pretty and asynchronous UIs but also to making it highly usable. Furthermore, the more useful the application is, the more productive the users will become, and the more lavish the praise that will be bestowed on us and our development teams, if applicable.