我正在尝试使用以下命令设置wpf控件的焦点:
[TestClass]
[TestFixture, Apartment(ApartmentState.STA)]
public class OnEnter_MoveFocus_Test
{
[TestInitialize]
public void TestInitialize()
{
if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
{
throw new ThreadStateException("The current threads apartment state is not STA");
}
}
[TestMethod]
public void TestMoveFocus()
{
TextBox TextBox1 = new TextBox() { Name = nameof(TextBox1), Focusable=true };
var result = TextBox1.Focus(); //results in false
Keyboard.Focus(TextBox1);
}
}
TextBox1.Focus();
返回false(显然TextBox1
可以关注并启用),使用Keyboard.Focus(TextBox1);
时,Keyboard.FocusedElement
仍为null
。
我该如何解决这个问题?