新しいウィンドウが開いたら、特定のテキストボックスにフォーカスしてテキスト全体を選択します。 私はこのチュートリアルのベースにそれを試してみました:TextBoxをフォーカスして、ウィンドウの初期化後にすべてのテキストを選択します。
<Grid d:DataContext="{StaticResource DesignTimeLayerViewModel1}" FocusManager.FocusedElement="{Binding ElementName=LayerNameInput}">
との相互作用の行動でそれを試してみました:私は私のグリッドにこれを使用する要素の焦点を合わせるためにhttps://blogs.msdn.microsoft.com/argumentnullexceptionblogpost/2013/04/12/a-simple-selectall-behavior-for-textboxes/
を
<TextBox x:Name="LayerNameInput"
Text="{Binding MapLayerName, UpdateSourceTrigger=PropertyChanged}"
VerticalContentAlignment="Center"
Width="240">
<i:Interaction.Behaviors>
<behaviors:SelectAllTextBoxBehavior></behaviors:SelectAllTextBoxBehavior>
</i:Interaction.Behaviors>
</TextBox>
行動規範:
public class SelectAllTextBoxBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.GotFocus += this.OnTextBoxGotFocus;
}
protected override void OnDetaching()
{
this.AssociatedObject.GotFocus -= this.OnTextBoxGotFocus;
base.OnDetaching();
}
private void OnTextBoxGotFocus(object sender, RoutedEventArgs e)
{
this.AssociatedObject.SelectAll();
}
}
問題はbindi ng。ウィンドウが作成されると、ビヘイビアは正しく発生しますが、実際にはTextBoxにはテキストはありません。 TextBoxが初期化され、テキストがバインドされた変数の値に設定され、選択が失われます。 複数回Tabキーを使用してTextBoxのフォーカスを再調整すると、正常に動作します。
TextBoxをフォーカスして、ウィンドウ作成時にテキスト全体を選択する方法はありますか?それの後ろに大量のコードがなければ?
ありがとうございます!