首先,我不认为这与获取TextBox
的其他帖子重复,将焦点设置为插入闪烁并具有键盘焦点。我已经尝试了我看到的每一种方法,但它们都没有用。
我有一个按钮,可以添加Listbox
的路径。列表框项是具有TextBlock
和Listbox
属性的自定义控件。通常它会显示TextBlock
。点击后,隐藏了TextBlock
,并且Listbox
可见以进行编辑。我想要发生的是当点击添加按钮时它应该创建一个项目,该项目应该处于编辑模式。当我这样做时,我得到了IBeam
,但它并没有闪烁,所以我认为它没有适当的焦点。我尝试了很多不同的方法来做到这一点。
当我点击该项目时,它可以正常工作。
以下是AddSearchPathMouseUp
:
/// <summary>
/// Handle Add search path mouse up
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="MouseButtonEventArgs" /> instance containing the event data.</param>
private void AddSearchPathMouseUp(object sender, MouseButtonEventArgs e)
{
SearchPathList.Focus();
SearchPathList.SelectedItem = null;
var mouseDownEvent = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
{
RoutedEvent = Mouse.MouseDownEvent
};
var mouseUpEvent = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
{
RoutedEvent = Mouse.MouseUpEvent
};
var path = Environment.GetEnvironmentVariable("Rtx64Dir") ?? Environment.GetEnvironmentVariable("SystemDrive") ?? "C:\\";
var di = new DirectoryInfo(path);
AddPathToList(di.Root.Name, true);
var listItem = SearchPathList.Items[SearchPathList.Items.Count - 1] as ListBoxItem;
if (listItem == null) return;
listItem.Focus();
lock (LockObject)
{
SearchPathList.SelectedIndex = SearchPathList.Items.Count - 1;
var browseEditText = listItem.Content as BrowseEditText;
if (browseEditText == null) return;
browseEditText.EditMode = true;
browseEditText.TextEditText.CaretIndex = browseEditText.Text.Length + 1;
browseEditText.RaiseEvent(mouseDownEvent);
browseEditText.RaiseEvent(mouseUpEvent);
}
}
以下是userControls.xaml
<UserControl x:Class="Rtx64ControlPanel.BrowseEditText"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="790">
<Grid Width="790">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBox x:Name="TextEditText" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Center" />
<TextBlock x:Name ="TextBlockText" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" VerticalAlignment="Center" />
<Button x:Name="BrowseButton" Grid.Row="0" Grid.Column="1" Content=" ... " HorizontalAlignment="Stretch" VerticalAlignment="Center" />
</Grid>
这是用户控件的代码隐藏
/// <summary>
/// Interaction logic for BrowseEditText.xaml
/// </summary>
public partial class BrowseEditText
{
private bool _editMode;
/// <summary>
/// Gets or sets a value indicating whether [edit mode].
/// </summary>
/// <value><c>true</c> if [edit mode]; otherwise, <c>false</c>.</value>
public bool EditMode
{
get { return _editMode; }
set
{
_editMode = value && Security.IsUserAdministrator;
TextBlockText.Visibility = _editMode ? Visibility.Hidden : Visibility.Visible;
TextEditText.Visibility = _editMode ? Visibility.Visible : Visibility.Hidden;
BrowseButton.Visibility = _editMode ? Visibility.Visible : Visibility.Hidden;
if (!_editMode) return;
TextEditText.Focusable = true;
TextEditText.Focus();
Keyboard.Focus(TextEditText);
}
}
private String _text;
/// <summary>
/// Gets or sets the text.
/// </summary>
/// <value>The text.</value>
public String Text
{
get { return _text; }
set
{
_text = value;
TextBlockText.Text = Text;
TextEditText.Text = Text;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="BrowseEditText"/> class.
/// </summary>
public BrowseEditText()
{
InitializeComponent();
EditMode = false;
}
}