实时循环计数器输出wpf

时间:2009-11-24 15:43:43

标签: wpf loops real-time

如果我想让一个文本框实时表示wpf中循环计数器的值,我该怎么办?

附加工作解决方案:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private delegate void UpdateTextBox(DependencyProperty dp, Object value);
...
private void MyMethod()
{
    ...
    int iMax=...;
    ...
    MyClass iMyClass = new MyClass(arguments);
        this.DataContext = iMyClass;
        UpdateTextBox updateTBox = new UpdateTextBox(textBlock1.SetValue);
        for (int i = 1; i <= iMax; i++)
        {
            iMyClass.MyClassMethod(i);
            Dispatcher.Invoke(updateTBox, System.Windows.Threading.DispatcherPriority.Background, new object[] { MyClass.MyPropertyProperty, iMyClass.myProperty });

        }

这是我根据你的建议尝试的代码,但它不起作用,我在文本框中写了“0”,所以我认为绑定是正常的,但循环不起作用。我还让循环通过循环内的textbox2.text =“a”直接写入另一个文本框,但它也没有用。

/// <summary>
/// Interaction logic for Window1.xaml
/// DOESNT WORK PROPERLY
/// </summary>
public partial class Window1 : Window
{


    public Window1()
    {
        InitializeComponent();
        TestClass tTest = new TestClass();
        this.DataContext = tTest ;
        tTest.StartLoop();
    }
}
public class TestClass : DependencyObject
{
    public TestClass()
    {
        bwLoop = new BackgroundWorker();

        bwLoop.DoWork += (sender, args) =>
        {

            // do your loop here -- this happens in a separate thread
            for (int i = 0; i < 10000; i++)
            {
                LoopCounter=i;
            }
        };
    }
    BackgroundWorker bwLoop;

    public int LoopCounter
    {
        get { return (int)GetValue(LoopCounterProperty); }
        set { SetValue(LoopCounterProperty, value); }
    }

    public static readonly DependencyProperty LoopCounterProperty = DependencyProperty.Register("LoopCounter", typeof(int), typeof(TestClass));

    public void StartLoop()
    {
        bwLoop.RunWorkerAsync();
    }
}

}

2 个答案:

答案 0 :(得分:2)

  1. 在后台进程中运行循环(以便在循环运行时UI可以自行更新)和

  2. 将循环计数器写入属性(带有INotifyPropertyChanged的DependencyProperty或CLR属性),该属性绑定到用户界面中的TextBox。或者,您可以通过Dispatcher.Invoke直接更改TextBox的值(但这不太优雅)。

  3. 这有帮助吗?随意要求澄清......


    代码示例(未经测试),使用DependencyProperty(必须绑定到TextBox):

    BackgroundWorker bwLoop;
    public static readonly DependencyProperty LoopCounterProperty = 
        DependencyProperty.Register("LoopCounter", typeof(int),
        typeof(Window1), new FrameworkPropertyMetadata(0));
    
    public int LoopCounter  {
        get { return (int)this.GetValue(LoopCounterProperty); }
        set { this.SetValue(LoopCounterProperty, value); } 
    }
    
    private MyWindow() {
        ...
        bwLoop = new BackgroundWorker();
    
        bwLoop.DoWork += (sender, args) => {
            ...
            for (int i = 0; i < someLimit; i++) {
                Dispatcher.Invoke(new Action(() => LoopCounter=i));
                System.Threading.Thread.Sleep(250); // do your work here
            }
        }
    
        bwLoop.RunWorkerCompleted += (sender, args) => {
            if (args.Error != null)
                MessageBox.Show(args.Error.ToString());
        };
    }
    
    private void StartLoop() {
        bwLoop.RunWorkerAsync();
    }
    

答案 1 :(得分:0)

您可以使用数据绑定来持续更新文本。