//-----------------------------------------------------------------------
//
// Copyright (c) 2009 Bryant Likes
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// Read more about the MIT License for software at it's wikipedia
// page here: http://en.wikipedia.org/wiki/MIT_License
//
//
//-----------------------------------------------------------------------
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace Bml.Controls
{
///
/// A simple content control to enable a default command when the user hits the enter key
///
public class FormControl : ContentControl
{
public ICommand DefaultCommand
{
get { return (ICommand)GetValue(DefaultCommandProperty); }
set { SetValue(DefaultCommandProperty, value); }
}
public static readonly DependencyProperty DefaultCommandProperty =
DependencyProperty.Register("DefaultCommand", typeof(ICommand), typeof(FormControl),
null);
protected override void OnContentChanged(object oldContent, object newContent)
{
var content = newContent as FrameworkElement;
if (content == null) return;
var elems = content.FindChildren();
foreach (var el in elems)
{
el.KeyUp += new KeyEventHandler(Element_KeyUp);
}
base.OnContentChanged(oldContent, newContent);
}
void Element_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// Ugly hack since the textbox and password box only
// update their binding when they lose focus
if (sender is TextBox || sender is PasswordBox)
{
this.Focus();
}
Dispatcher.BeginInvoke(ExecuteCommand);
}
}
private void ExecuteCommand()
{
DefaultCommand.Execute(null);
}
}
public static class Extensions
{
public static List FindChildren(this DependencyObject element)
where T : FrameworkElement
{
var results = new List();
int count = VisualTreeHelper.GetChildrenCount(element);
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(element, i);
if (child is T)
{
results.Add(child as T);
}
else
{
int childCount = VisualTreeHelper.GetChildrenCount(child);
if (childCount > 0)
{
results.AddRange(child.FindChildren());
}
}
}
return results;
}
}
}