[英]How to select text of a texbox in wpf MVVM
我在堆棧面板中有一些控制權。
當我右鍵單擊面板時,它會給我一個上下文菜單,並且我可以編輯這些控件的文本。
在這里,我使用了TextBlock來顯示數據,並使用TextBox來編輯數據(當可見TextBox時,TextBlock變為折疊狀態,反之亦然)
我想選擇TextBox的所有文本,並在顯示TextBox時將其聚焦。
我嘗試使用互動。 但是沒有解決:(
有什么辦法嗎?
例如:當TextBox可見時,我可以在視圖模型中觸發一些命令,然后從視圖模型中選擇所有文本。
<TextBlock Text="{Binding MachineResponseText}" Visibility="{Binding IsEditing, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=true}"/>
<TextBox x:Name="MachineResponseTextBox" Text="{Binding MachineResponseText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Visibility="{Binding IsEditing, Converter={StaticResource BoolToVisibilityConverter}}">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsVisible" Value="True">
<!--Is there any way to select all the text when this textbox is visible?-->
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
這不是您問題的完整答案,更多的是朝着正確的方向發展。
將GotKeyboardFocus處理程序添加到您的文本框並運行textbox.SelectAll(),即
XAML部分:
<StackPanel>
<Button>B1</Button>
<TextBox Text="Test123" GotKeyboardFocus="TextBox_GotKeyboardFocus"/>
<TextBlock>123</TextBlock>
<Button>B2</Button>
</StackPanel>
后面的代碼:
private void TextBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
var textBox = (TextBox) sender;
if (textBox.SelectionLength == 0)
{
textBox.SelectAll();
}
}
您可能需要通過調用來處理鍵盤焦點以及TextBox.Focus()
在切換到編輯模式,或者更MVVM方式解釋這里
有一個事件可以使用,並且它會附加在所有UIElement
UIElement.IsVisibleChanged中。之后,您必須在文本框中指定焦點並選擇這樣的文本
private void TextBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
this.myTextBox.Focus();
this.myTextBox.SelectAll();
}
這是版本代碼,您可以通過在視圖模型中觸發事件在mvvm上下文中執行相同的操作。
如果要在mvvm中設置文本框焦點,此stackoverflow帖子可能會對您有所幫助。
如果您想使用MVVM設計模式,建議使用這種方式。
首先,您必須創建一個擴展方法,如下所示。
public static class IsSelectTextExtension { public static readonly DependencyProperty IsSelectTextProperty = DependencyProperty.RegisterAttached("IsSelectText", typeof(bool), typeof(IsSelectTextExtension), new UIPropertyMetadata(false, OnIsSelectTextPropertyChanged)); public static bool GetIsSelectText(DependencyObject obj) { return (bool)obj.GetValue(IsSelectTextProperty); } public static void SetIsSelectText(DependencyObject obj, bool value) { obj.SetValue(IsSelectTextProperty, value); } private static void OnIsSelectTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var uie = (TextBox)d; if ((bool)e.NewValue) { uie.SelectAll(); } } }
<Window x:Class="POS.MainWindow" 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" xmlns:local="clr-namespace:WpfExample" xmlns:extension="clr-namespace:WpfExample.Extension" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <TextBox extension:IsSelectTextExtension.IsSelectText="{Binding IsSelectText}"/> </Grid> </Window>
在視圖模型類中,定義IsSelectText屬性,然后根據需要更改值。
public class MainWindowViewModel: INotifyPropertyChanged { private bool _isSelectText; public bool IsSelectText { get { return this._isSelectText; } set { _isSelectText = value; OnPropertyChanged(nameof(IsSelectText)); } } public NewSaleViewModel() { } #region INotifyPropertyChanged Implimentations public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } #endregion }
您所要求的通常是首選的TextBox
行為。 您可以為App.xaml.cs
所有文本框更改此設置:
protected override void OnStartup(StartupEventArgs e)
{
// ...
EventManager.RegisterClassHandler(typeof(TextBox),
UIElement.GotFocusEvent, new RoutedEventHandler(OnTextBoxGotFocus));
}
private static void OnTextBoxGotFocus(object sender, RoutedEventArgs e)
{
if (sender is TextBox textBox && !textBox.IsReadOnly)
{
textBox.SelectAll();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.