wpf TextBox.IsFocused 属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13989378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TextBox.IsFocused property?
提问by Taras
I want to change a textBox.textonly if it is not in focus. Example:
textBox.text只有当它不在焦点时,我才想改变它。例子:
public void updateValue()
{
if (!this.valueTB.IsFocused)
this.valueTB.Text = updatedValue.ToString();
}
But the problem is that this.valueTB.IsFocusedproperty changes only when it's actually focused but not when it's focused out.
但问题是this.valueTB.IsFocused属性仅在实际聚焦时才会发生变化,而在聚焦时不会发生变化。
采纳答案by Jan Kohlhase
maybe you could take a bool value which tells you if the TB is focused or not:
也许您可以使用 bool 值来告诉您 TB 是否集中:
bool TBIsFocused = false;
private void valueTB_Enter(object sender, EventArgs e)
{
TBIsFocused = true;
}
private void valueTB_Leave(object sender, EventArgs e)
{
TBIsFocused = false;
}
回答by Clemens
回答by SidPen
the problem is that IsFocusedevent gets fired only once when it gets the focus thats it and then it doesn't fire. I think you want it to fire this event continuous till Textboxhas the focus. For that you have to keep on checking whether it has the focus or not at specific interval.
问题是IsFocused事件仅在获得焦点时才触发一次,然后不会触发。我认为您希望它连续触发此事件直到Textbox获得焦点。为此,您必须以特定的时间间隔继续检查它是否具有焦点。

