Created
June 6, 2012 05:37
-
-
Save jpolvora/2880087 to your computer and use it in GitHub Desktop.
FocusResult
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class CaliburnExtensions | |
{ | |
public static IEnumerable<IResult> BeginExecute(this IEnumerable<Func<IResult>> routines) | |
{ | |
return routines.Select(routine => routine()); | |
} | |
public static void AsCoroutine(this IEnumerable<IResult> routine, | |
EventHandler<ResultCompletionEventArgs> callBack = null, | |
ActionExecutionContext ctx = null) | |
{ | |
if (routine != null) | |
Coroutine.BeginExecute(routine.GetEnumerator(), ctx, callBack); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FocusResult : Result | |
{ | |
private readonly string _controlToFocus; | |
public FocusResult(string controlToFocus) | |
{ | |
_controlToFocus = controlToFocus; | |
} | |
public override void Execute(ActionExecutionContext context) | |
{ | |
var view = context.View as UserControl; | |
// add support for further controls here | |
var editableControls = GetChildrenByType<Control>(view, c => c is CheckBox || | |
c is TextBox || | |
c is Button); | |
var control = editableControls.SingleOrDefault(c => c.Name == _controlToFocus); | |
if (control != null) | |
{ | |
Caliburn.Micro.Execute.OnUIThread(() => | |
{ | |
control.Focus(); | |
var textBox = control as TextBox; | |
if (textBox != null) | |
textBox.Select(textBox.Text.Length, 0); | |
}); | |
} | |
RaiseCompleted(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Result : IResult | |
{ | |
public static List<T> GetChildrenByType<T>(UIElement element, | |
Func<T, bool> condition) where T : UIElement | |
{ | |
List<T> results = new List<T>(); | |
GetChildrenByType<T>(element, condition, results); | |
return results; | |
} | |
private static void GetChildrenByType<T>(UIElement element, | |
Func<T, bool> condition, List<T> results) where T : UIElement | |
{ | |
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) | |
{ | |
UIElement child = VisualTreeHelper.GetChild(element, i) as UIElement; | |
if (child != null) | |
{ | |
T t = child as T; | |
if (t != null) | |
{ | |
if (condition == null) | |
results.Add(t); | |
else if (condition(t)) | |
results.Add(t); | |
} | |
GetChildrenByType<T>(child, condition, results); | |
} | |
} | |
} | |
public abstract void Execute(ActionExecutionContext context); | |
public event EventHandler<ResultCompletionEventArgs> Completed; | |
public void RaiseCompleted() | |
{ | |
var handler = Completed; | |
if (handler != null) handler(this, new ResultCompletionEventArgs()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ommited code */ | |
public string ControlToFocus { get; set; } | |
protected override void OnViewLoaded(object view) | |
{ | |
base.OnViewLoaded(view); | |
if (string.IsNullOrEmpty(ControlToFocus)) return; | |
var newCtx = new ActionExecutionContext { View = (DependencyObject)view, Target = this }; | |
new IResult[] { new FocusResult(ControlToFocus) } | |
.AsCoroutine(ctx: newCtx); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment