其实是一个很弱弱的问题,我希望文本框的内容从0 变到100 是一个渐变的过程, step 为 1 ,可以清楚的看到 1,2, ,100. 当我用for 语句的是否发现并不是这样的效果。  有人可以帮我解答下吗?                    for (int i = 1; i < 100; i++)
                    {
                        tbBalance.Text = (100- i).ToString();
                        tbCredit.Text = (0+ i).ToString();
                      //  System.Threading.Thread.Sleep(100);
                    }或者需要单独开一个线程来显示?WPF 文本框

解决方案 »

  1.   

    模拟一个DoEvents
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System;
    using System.Security.Permissions;
    using System.Windows.Threading;
    namespace WpfApplication2
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, RoutedEventArgs e)
            {
                for (int i = 1; i < 100; i++)
                {
                    tbBalance.Text = (100 - i).ToString();
                    tbCredit.Text = (0 + i).ToString();
                    DispatcherHelper.DoEvents();
                    System.Threading.Thread.Sleep(100);
                    
                }
     
            }
    public class DispatcherHelper
    {
        /// <summary>
        /// Simulate Application.DoEvents function of <see cref=" System.Windows.Forms.Application"/> class.
        /// </summary>
        [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        public static void DoEvents()
        {
            var frame = new DispatcherFrame();
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
                new DispatcherOperationCallback(ExitFrames), frame);        try
            {
                Dispatcher.PushFrame(frame);
            }
            catch (InvalidOperationException)
            {
            }
        }    private static object ExitFrames(object frame)
        {
            ((DispatcherFrame)frame).Continue = false;
            return null;
        }
    }
        }
    }