2009-11-24 4 views
1

wpf의 루프 카운터 값을 실시간으로 나타내는 텍스트 상자를 원한다면 어떻게해야합니까?실시간 루프 카운터 출력 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"텍스트 상자에 기록 얻을 나던, 그래서 바인딩은 OK입니다 생각하지만, 루프가 작동하지 않습니다. 루프 내에서 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
  1. 배경 프로세스 루프를 실행하고

  2. 이 속성에 루프 카운터 물품 (에서 INotifyPropertyChanged으로하는 DependencyProperty 또는 CLR 속성)한다 (루프 실행 중에 UI가 자동으로 업데이트 할 수 있도록) 사용자 인터페이스의 TextBox에 바인딩됩니다. 또는 Dispatcher.Invoke를 통해 TextBox의 값을 직접 변경할 수도 있습니다 (그래도 덜 우아합니다).

(텍스트 상자에 바인딩해야 함)은 DependencyProperty를 사용 (테스트되지 않은) ...


코드 예제를 설명을 요청 주시기 바랍니다 :

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(); 
} 
+0

내가 DependancyProperty 및 디스패처와 함께 할 관리 .Invoke. 첫 번째 가능성에 대해 더 자세히 설명해 주시겠습니까? 백그라운드 프로세스에서 루프를 실행하고 종속성 속성에 루프 카운터를 작성하십시오. 내가 한 짓과 다른거야? – butaro

+0

코드 예제를 추가하여 코드와 비교할 수 있습니다. 질문에 코드를 추가하면 그에 대한 의견을 말할 수 있습니다. – Heinzi

+0

그랬지, 내가 한 일이 분명 할까? – butaro

0

당신은 데이터가 지속적으로 업데이트되는 텍스트에 대한 바인딩 사용할 수 있습니다.