Skip to content

Instantly share code, notes, and snippets.

@jpolvora
Last active December 11, 2015 08:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpolvora/4576803 to your computer and use it in GitHub Desktop.
Save jpolvora/4576803 to your computer and use it in GitHub Desktop.
<UserControl x:Class="SCO.Ria.UI.Views.LoginView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
xmlns:interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:local="clr-namespace:Caliburn.Micro.Focus;assembly=Caliburn.Micro.Focus"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" mc:Ignorable="d"
d:DesignHeight="154" d:DesignWidth="468">
<interactivity:Interaction.Behaviors>
<local:TabNextBehavior/>
</interactivity:Interaction.Behaviors>
<toolkit:BusyIndicator x:Name="IsBusy">
<toolkit:BusyIndicator.BusyContent>
<TextBlock Text="Verificando ..." />
</toolkit:BusyIndicator.BusyContent>
<Grid Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition Height="40" />
<RowDefinition Height="100" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="Usuário:" Grid.Column="0" Margin="5" />
<TextBox x:Name="Model_Username" Grid.Column="1" Margin="5" Width="200" />
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="Senha:" Grid.Column="0" Margin="5" />
<PasswordBox x:Name="Model_Password" Grid.Column="1" Margin="5" Width="200" />
</Grid>
<sdk:ValidationSummary Grid.Row="2" IsTabStop="False" />
<StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
<Button Content="Entrar" x:Name="LoginButton" Grid.Row="2" Width="100" Height="25" Margin="5" />
<Button Content="Cancelar" x:Name="CancelButton" Grid.Row="2" Width="100" Height="25" Margin="5" />
</StackPanel>
</Grid>
</toolkit:BusyIndicator>
</UserControl>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
namespace Caliburn.Micro.Focus {
public class TabNextBehavior : Behavior<Control> {
protected override void OnAttached() {
base.OnAttached();
this.AssociatedObject.KeyUp += AssociatedObject_KeyUp;
}
protected override void OnDetaching() {
base.OnDetaching();
this.AssociatedObject.KeyUp -= AssociatedObject_KeyUp;
}
void AssociatedObject_KeyUp(object sender, KeyEventArgs args) {
if (args.Key == System.Windows.Input.Key.Enter) {
DependencyObject parent = null;
if (AssociatedObject is ChildWindow)
parent = ((ChildWindow)parent).Content as DependencyObject;
else parent = AssociatedObject;
parent.TabNext(); //extensin Method from VisualTreeExtensions.cs
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace Caliburn.Micro.Focus {
public static class VisualTreeExtensions {
public static T GetAncestor<T>(this FrameworkElement source) where T : class {
var current = source;
while (current != null) {
if (current is T) return current as T;
current = current.Parent as FrameworkElement;
}
return null;
}
public static List<T> FindControlsRecursive<T>(this DependencyObject container)
where T : DependencyObject {
if (container == null) return null;
var foundControls = new List<T>();
//for each child object in the container
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(container); i++) {
var control = VisualTreeHelper.GetChild(container, i);
if (control is T)
foundControls.Add((T)control);
if (VisualTreeHelper.GetChildrenCount(control) > 0) {
foundControls.AddRange(FindControlsRecursive<T>(control));
}
}
return foundControls;
}
public static Control FindControlByName(this DependencyObject container, string propertyName) {
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(container); i++) {
var child = VisualTreeHelper.GetChild(container, i);
var control = child as Control;
if (control != null) {
if (control.Name == propertyName)
return control;
}
if (VisualTreeHelper.GetChildrenCount(child) > 0) {
return FindControlByName(child, propertyName);
}
}
return null;
}
public static void TabNext(this DependencyObject parentElement) {
//CSharpExtensions.Guard(() => parentElement != null, "parentElement");
var fromControl = FocusManager.GetFocusedElement() as Control;
if (fromControl == null)
return;
var children = parentElement.FindControlsRecursive<Control>()
.Where(c => c.IsEnabled && c.IsTabStop && c.Visibility == Visibility.Visible)
.ToList();
if (children.Contains(fromControl)) {
var thisIndex = children.IndexOf(fromControl);
var targetIndex = thisIndex + 1;
if (children.Count > targetIndex) {
var targetChild = children[targetIndex];
fromControl.Dispatcher.BeginInvoke(() => {
targetChild.Focus();
var txt = targetChild as TextBox;
if (txt != null) {
txt.SelectAll();
}
});
}
}
}
}
}
@jpolvora
Copy link
Author

@kamranayub
Copy link

What timing! I need this. Is there a reason you put these into Caliburn's namespace?

@sleimanzublidi
Copy link

Perfect! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment