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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:43:22  来源:igfitidea点击:

TextBox.IsFocused property?

c#wpftextbox

提问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

The IsFocusedproperty certainly changes its value when the TextBox gets or loses the focus.

IsFocused当 TextBox 获得或失去焦点时,该属性肯定会更改其值。

But TextBox also provides the GotFocusand LostFocusevents.

但 TextBox 还提供GotFocusLostFocus事件。

回答by Inkey

You can use the textbox focus event Lost

您可以使用文本框焦点事件Lost

And GotFocus

并且得到焦点

回答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获得焦点。为此,您必须以特定的时间间隔继续检查它是否具有焦点。

回答by Ionic? Biz?u

Use only IsFocusedpropery:

仅使用IsFocused属性:

public void updateValue()
{
     if((!tb.IsFocused) || (tb.IsFocused == false))
     {
          tb.Text = "The text was updated when I wasn't focused. Is't OK?";
     }
}