Class DefaultFocusBehavior
Implements a behavior to set the default focused element.
Inheritance
Implements
Namespace: Sartorius.SAF.Presentation.Controls.Behaviors
Assembly: Sartorius.SAF.Presentation.Controls.dll
Syntax
public class DefaultFocusBehavior : Behavior<FrameworkElement>, IAnimatable, IAttachedObject
Remarks
You can specifically attach this behavior to the element that should be focused, attach it to a container and set the element that should be focused by setting FocusedElement or attach it to a container without setting FocusedElement. In the latter case the first focusable child is focused. This behavior recognizes the following children as focusable:
- System.Windows.Controls.TextBox
- System.Windows.Controls.CheckBox
- System.Windows.Controls.ComboBox
- System.Windows.Controls.Button
- System.Windows.Controls.ListView
- System.Windows.Controls.TreeView
- System.Windows.Controls.ListBox
- System.Windows.Controls.PasswordBox
Examples
This example illustrates how to use the DefaultFocusBehavior to focus the first input control, in this case the WatermarkTextBox.
<HeaderedContentControl xmlns:SAF="http://www.sartorius.com/SAF"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors">
<b:Interaction.Behaviors>
<SAF:DefaultFocusBehavior />
</b:Interaction.Behaviors>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="20" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"
MinHeight="28" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<SAF:Caption Grid.Row="0"
Grid.Column="0"
Style="{DynamicResource SAF_ContentBoxCaption}"
Text="Name" />
<!-- This element gets the focus by default. -->
<SAF:WatermarkTextBox Grid.Row="0"
Grid.Column="2"
Watermark="Max Mustermann"
Style="{SAF:Style WatermarkTextBoxSingleLine}"
Text="{Binding Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
<SAF:Caption Grid.Row="1"
Grid.Column="0"
Style="{DynamicResource SAF_ContentBoxCaption}"
Text="Description" />
<TextBox Grid.Row="1"
Grid.Column="2"
Style="{SAF:Style TextBoxMultiLine}"
Text="{Binding Description, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
</Grid>
</HeaderedContentControl>
This example illustrates how to use the DefaultFocusBehavior to focus a specific control by setting FocusedElement.
<HeaderedContentControl xmlns:SAF="http://www.sartorius.com/SAF"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors">
<b:Interaction.Behaviors>
<SAF:DefaultFocusBehavior FocusedElement="{Binding ElementName=DescriptionTextBox}" />
</b:Interaction.Behaviors>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="20" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"
MinHeight="28" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<SAF:Caption Grid.Row="0"
Grid.Column="0"
Style="{DynamicResource SAF_ContentBoxCaption}"
Text="Name" />
<SAF:WatermarkTextBox Grid.Row="0"
Grid.Column="2"
Watermark="Max Mustermann"
Style="{SAF:Style WatermarkTextBoxSingleLine}"
Text="{Binding Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
<SAF:Caption Grid.Row="1"
Grid.Column="0"
Style="{DynamicResource SAF_ContentBoxCaption}"
Text="Description" />
<!-- This element gets the focus as it is defined by the FocusedElement property. -->
<TextBox x:Name="DescriptionTextBox"
Grid.Row="1"
Grid.Column="2"
Style="{SAF:Style TextBoxMultiLine}"
Text="{Binding Description, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
</Grid>
</HeaderedContentControl>
This example illustrates how to use the DefaultFocusBehavior to focus a specific control.
<HeaderedContentControl xmlns:SAF="http://www.sartorius.com/SAF"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="20" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"
MinHeight="28" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<SAF:Caption Grid.Row="0"
Grid.Column="0"
Style="{DynamicResource SAF_ContentBoxCaption}"
Text="Name" />
<SAF:WatermarkTextBox Grid.Row="0"
Grid.Column="2"
Watermark="Max Mustermann"
Style="{SAF:Style WatermarkTextBoxSingleLine}"
Text="{Binding Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
<SAF:Caption Grid.Row="1"
Grid.Column="0"
Style="{DynamicResource SAF_ContentBoxCaption}"
Text="Description" />
<!-- This element gets the focus as it is defined by the FocusedElement property. -->
<TextBox Grid.Row="1"
Grid.Column="2"
Style="{SAF:Style TextBoxMultiLine}"
Text="{Binding Description, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}">
<b:Interaction.Behaviors>
<SAF:DefaultFocusBehavior />
</b:Interaction.Behaviors>
</TextBox>
</Grid>
</HeaderedContentControl>
Fields
View SourceFocusedElementProperty
Identifies the System.Windows.DependencyProperty for the FocusedElement property.
Declaration
public static readonly DependencyProperty FocusedElementProperty
Field Value
Type | Description |
---|---|
System.Windows.DependencyProperty |
Properties
View SourceFocusedElement
Gets or sets the element that should be focused.
Declaration
public FrameworkElement FocusedElement { get; set; }
Property Value
Type | Description |
---|---|
System.Windows.FrameworkElement | The element that should be focused. |
Methods
View SourceIsFocusableInputElement(FrameworkElement)
Checks whether the given element
is focusable.
Declaration
protected virtual bool IsFocusableInputElement(FrameworkElement element)
Parameters
Type | Name | Description |
---|---|---|
System.Windows.FrameworkElement | element | The System.Windows.FrameworkElement |
Returns
Type | Description |
---|---|
System.Boolean | True if the |
OnAttached()
Called after the behavior is attached to an AssociatedObject.
Declaration
protected override void OnAttached()
Overrides
Remarks
Override this to hook up functionality to the AssociatedObject.
OnDetaching()
Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
Declaration
protected override void OnDetaching()
Overrides
Remarks
Override this to unhook functionality from the AssociatedObject.
OnLoaded(Object, EventArgs)
Called when the Microsoft.Xaml.Behaviors.Behavior.AssociatedObject is loaded.
Declaration
protected virtual void OnLoaded(object sender, EventArgs args)
Parameters
Type | Name | Description |
---|---|---|
System.Object | sender | The sender. |
System.EventArgs | args | The System.EventArgs. |