using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication1
{
public static class TextBoxProperties
{
public static readonly DependencyProperty SelectAllProperty =
DependencyProperty.RegisterAttached("SelectAll", typeof(bool),
typeof(TextBoxProperties), new PropertyMetadata(false, OnSelectAllChanged));
public static void SetSelectAll(DependencyObject o, bool value)
{
o.SetValue(SelectAllProperty, value);
}
public static bool GetSelectAll(DependencyObject o)
{
return (bool)o.GetValue(SelectAllProperty);
}
private static void OnSelectAllChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue == true)
{
((TextBox)d).GotFocus += new RoutedEventHandler(TextBoxProperties_GotFocus);
}
}
private static void TextBoxProperties_GotFocus(object sender, RoutedEventArgs e)
{
((TextBox)sender).SelectAll();
}
}
}
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
namespace SilverlightApplication1
{
public class SelectAllBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.GotFocus += new RoutedEventHandler(AssociatedObject_GotFocus);
}
void AssociatedObject_GotFocus(object sender, RoutedEventArgs e)
{
((TextBox)sender).SelectAll();
}
}
}
<UserControl x:Class="SilverlightApplication1.MainPage3"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:behaviors="clr-namespace:SilverlightApplication1;assembly=SilverlightApplication1">
<Grid x:Name="LayoutRoot" Background="White">
<TextBox Name="textBox1" Width="120" Height="23" Text="computer" HorizontalAlignment="Left">
<i:Interaction.Behaviors>
<behaviors:SelectAllBehavior/>
</i:Interaction.Behaviors>
</TextBox>
<TextBox Name="textBox2" Width="120" Height="23" Text="中华人民共和国,china" HorizontalAlignment="Center" behaviors:TextBoxProperties.SelectAll="True"/>
</Grid>
</UserControl>