对于 WPF 文本框,我需要在鼠标单击它的情况下选择它的所有内容(在“现实生活”中,它将在触摸板显示器上触摸)。 为了进行测试,我创建了简单的项目并检查 Stackoverflow 上的解决方案。我发现了几个案例,但都不起作用。请帮我?我在 Github 上的项目:https://github.com/cheffcook/CustomTextBox2
附注关于 OnKeyDown - 我只是测试另一个功能
扩展文本框.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace CustomTextBox
{
public class ExtendedTextBox : TextBox
{
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (TextProperty != null && e.Key == Key.Enter)
{
GetBindingExpression(TextProperty)?.UpdateSource();
}
}
protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);
SelectAll();
}
/*protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
base.OnGotKeyboardFocus(e);
SelectAll();
}*/
protected override void OnMouseUp(MouseButtonEventArgs e)
{
base.OnMouseUp(e);
Focus();
}
}
}
主窗口.xaml
<Window x:Class="CustomTextBox.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:localVM="clr-namespace:CustomTextBox"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<localVM:MainViewModel/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0" Height="40" Width="80" Content="Nothing" HorizontalAlignment="Left" />
<Label Grid.Row="1" Grid.Column="1" FontSize="20" Content="{Binding CustomValue1}" />
<Label Grid.Row="2" Grid.Column="1" FontSize="20" Content="{Binding CustomValue2}" />
<Label Grid.Row="3" Grid.Column="1" FontSize="20" Content="{Binding CustomValue3}" />
<localVM:ExtendedTextBox Grid.Row="1" Grid.Column="0" FontSize="20" Text="{Binding CustomValue1}" />
<localVM:ExtendedTextBox Grid.Row="2" Grid.Column="0" FontSize="20" Text="{Binding CustomValue2}" />
<localVM:ExtendedTextBox Grid.Row="3" Grid.Column="0" FontSize="20" Text="{Binding CustomValue3}" />
</Grid>
</Window>
MainViewModel.cs
using Prism.Mvvm;
namespace CustomTextBox
{
internal class MainViewModel : BindableBase
{
private float _customValue1;
public float CustomValue1
{
get => _customValue1;
set => SetProperty(ref _customValue1, value, nameof(CustomValue1));
}
private float _customValue2;
public float CustomValue2
{
get => _customValue2;
set => SetProperty(ref _customValue2, value, nameof(CustomValue2));
}
private float _customValue3;
public float CustomValue3
{
get => _customValue3;
set => SetProperty(ref _customValue3, value, nameof(CustomValue3));
}
public MainViewModel()
{
CustomValue1 = 12.3f;
CustomValue2 = 45.6f;
CustomValue3 = 78.9f;
}
}
}
通过添加
SelectAll()
到OnMouseUp Event
它可以工作
protected override void OnMouseUp(MouseButtonEventArgs e)
{
base.OnMouseUp(e);
Focus();
SelectAll();
}