2009/06/27(Sat) 01:04:26 編集(投稿者)
public class AllSelectTextBehavior
{
public static bool GetIsEnabled(ListView view)
{
return (bool)view.GetValue(IsEnabledProperty);
}
public static void SetIsEnabled(ListView view, bool value)
{
view.SetValue(IsEnabledProperty, value);
}
public static readonly DependencyProperty IsEnabledProperty =
DependencyProperty.RegisterAttached(
"IsEnabled", typeof(bool), typeof(AllSelectTextBehavior),
new UIPropertyMetadata(false, OnIsEnabledChanged));
static void OnIsEnabledChanged(DependencyObject depObj,
DependencyPropertyChangedEventArgs e)
{
var textBox = depObj as TextBox;
if (textBox == null)
{
return;
}
if (e.NewValue is bool == false)
{
return;
}
bool isEnabled = (bool)e.NewValue;
if (isEnabled)
{
textBox.PreviewMouseLeftButtonDown += OnSelectMouseDown;
textBox.GotFocus += GotFocus;
textBox.LostFocus += LostFocus;
}
else
{
textBox.PreviewMouseLeftButtonDown -= OnSelectMouseDown;
textBox.GotFocus -= GotFocus;
textBox.LostFocus -= LostFocus;
}
}
static public void GotFocus(object o, RoutedEventArgs e)
{
TextBox textBox = o as TextBox;
if (textBox == null)
return;
textBox.SelectAll();
textBox.Tag = true;
}
static public void LostFocus(object o, RoutedEventArgs e)
{
TextBox textBox = o as System.Windows.Controls.TextBox;
textBox.Tag = false;
}
static public void OnSelectMouseDown(object o, RoutedEventArgs e)
{
TextBox textBox = o as System.Windows.Controls.TextBox;
if (textBox == null)
return;
if (textBox.Tag == null || (bool)textBox.Tag == false)
{
textBox.SelectAll();
textBox.Focus();
e.Handled = true;
textBox.Tag = true;
}
}
Behaviorバージョンです。
ただしかし、マウスのクリックでDoDragを実装しているとけんかして、期待通りの動作をしてくれません。
ぬーん、ちょっときもちわるいー