wpf C# 如何使文本框闪烁

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20288760/
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 10:08:10  来源:igfitidea点击:

C# How to blink TextBox

c#wpftextbox

提问by bircastri

I would like my TextBoxto blink intermittently.

我希望我TextBox的眼睛间歇性地眨眼。

I have this method

我有这个方法

private void abilitaAltroToken(int indiceRiga, Grid grid)
{
    UIElement element = grid.Children[indiceRiga];
    Label label = (Label)element;
    element = grid.Children[++indiceRiga];
    TextBox textBox = (TextBox)element;
    textBox.Background = Brushes.Blue;
    label.Background = Brushes.Blue;

    Thread.Sleep(1000);
    label.Background = Brushes.White;

    Thread.Sleep(1000);
    label.Background = Brushes.Blue;

    Thread.Sleep(1000);
    label.Background = Brushes.White;

    Thread.Sleep(1000);
    label.Background = Brushes.Blue;       
}

This code does not return an error but, the blinking doesn't occur.

此代码不会返回错误,但不会发生闪烁。

回答by sthotakura

First of all, you should not put Thread.Sleep in your Main (UI) thread code, that will put the UI thread to sleep, and you wouldn't see any changes happening on the UI.

首先,您不应该将 Thread.Sleep 放在您的主 (UI) 线程代码中,这会使 UI 线程进入睡眠状态,并且您将看不到 UI 上发生的任何更改。

Personally, I would use animations (that too in XAML) and Triggers/VisualStates to achieve what you are trying here.

就个人而言,我会使用动画(在 XAML 中也是如此)和触发器/VisualStates 来实现您在此处尝试的功能。

However, as I don't have much visibility of your XAML, Here is the procedural code to achieve blinking of label:

但是,由于我对您的 XAML 没有太多了解,以下是实现标签闪烁的程序代码:

var colorAnim = new ColorAnimationUsingKeyFrames()
{
    KeyFrames = new ColorKeyFrameCollection
            {
                new DiscreteColorKeyFrame(Colors.White, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.5))),
                new DiscreteColorKeyFrame(Colors.Blue, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1))),
                new DiscreteColorKeyFrame(Colors.White, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1.5))),
                new DiscreteColorKeyFrame(Colors.Blue, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2))),
            }
};

var storyBoard = new Storyboard();

storyBoard.Children.Add(colorAnim);
Storyboard.SetTarget(storyBoard, label);
Storyboard.SetTargetProperty(storyBoard, new PropertyPath("(Background).(SolidColorBrush.Color)"));

storyBoard.Begin();

Basically, I translated the following code snippet from your question:

基本上,我从您的问题中翻译了以下代码片段:

Thread.Sleep(1000);  
label.Background = Brushes.White;

Thread.Sleep(1000);  
label.Background = Brushes.Blue;

Thread.Sleep(1000);  
label.Background = Brushes.White;

Thread.Sleep(1000);  
label.Background = Brushes.Blue;

回答by Uri Goren

Use a TimerObject,

使用计时器对象,

Here's a sample code

这是一个示例代码

private static System.Timers.Timer aTimer;
private static bool blinkFlag;
private Label label;

private void abilitaAltroToken(int indiceRiga,Grid grid)
{
        UIElement element = grid.Children[indiceRiga];
        label = (Label)element;
        element = grid.Children[++indiceRiga];
        TextBox textBox = (TextBox)element;
        textBox.Background = Brushes.Blue;
        label.Background = Brushes.Blue;

    aTimer = new System.Timers.Timer(1000);

    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

    // Set the Interval to 1 seconds (1000 milliseconds).
    aTimer.Interval = 1000;
    aTimer.Enabled = true;

}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
   label.Background = (blinkFlag?Brushes.Blue:Brushes.White);
   blinkFlag=!blinkFlag;
   aTimer.Interval = 1000;
   aTimer.Enabled = true;
}

回答by Roger Deep

Double click on the timer toolbox or drag a timer to the form. Add the following code:

双击计时器工具箱或将计时器拖到表单中。添加以下代码:

private void timer1_Tick(object sender, EventArgs e)
{
    if (tickcolor) txtAno.BackColor = Color.Red;
    if (!tickcolor) txtAno.BackColor = Color.White;
    tickcolor = !tickcolor;
}

private void txtAno_KeyPress(object sender, KeyPressEventArgs e)
{
    timer1.Stop();
    txtAno.BackColor = Color.White;
}

In the form_load event add this:

在 form_load 事件中添加:

    timer1.Interval = 500;
    timer1.Start();

    txtAno.Focus();

Declare the variable: private bool tickcolor = true;

声明变量: private bool tickcolor = true;

This will make the textbox txtAno to flash red and white.

这将使文本框 txtAno 闪烁红色和白色。

回答by odyss-jii

You should not be sleeping in the "UI-thread". I suggest that you try using a DispatcherTimerinstead. See the following link for more information: http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer%28v=vs.110%29.aspx

您不应该在“UI 线程”中睡觉。我建议您尝试使用 aDispatcherTimer代替。有关详细信息,请参阅以下链接:http: //msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer%28v=vs.110%29.aspx