2014-04-01 26 views
-1

我在WPF和DispatcherTimer中有一个表单,每当tick事件触发时,我想将OrderLbl.Text的值从“今日订单”更改为“本周订单”,并将“本周订单”更改为“本月Orders“如何更改WPF中Textblock的文本属性?

但是,当我尝试从_timer_Tick事件中更改OrderLbl.text的值时,它会引发一个异常,指出需要引用对象,但是当我在tick事件中引用它时,它不会更改的OrderLbl.Text

代码值低于;

public void Start() 
    { 

     System.Windows.Threading.DispatcherTimer DTimer = new System.Windows.Threading.DispatcherTimer(); 
     DTimer.Tick += new EventHandler(_timer_Tick); 
     DTimer.Interval = new TimeSpan(0, 0, 5); 
     DTimer.Start(); 


    } 

    private static void _timer_Tick(Object sender, EventArgs e) 
    { 

     if (OrderLbl.Text == "Today's Orders") 
     { 
      OrderLbl.Text = "This Week's Orders"; 

     } 


     else if (OrderLbl.Text == "This Week's Orders") 
     { 
      OrderLbl.Text = "This Month's Orders"; 

     } 

     //else 
     //{ 
     // mw.orderlbl.text= "today's orders"; 
     // go 
     //} 

    } 
+0

您确定您的文本框名称是correcT吗? OrderLbl听起来是一个标签 – Sajeetharan

+0

是的,我特意做了这件事,因为我希望我的TextBlock能够模拟标签的功能。 –

回答

0

ř从Tick处理器中删除static关键字:

private void _timer_Tick(Object sender, EventArgs e) 
{ 
    ... 
} 
相关问题