依赖属性自动更新(带计时器的日期时间)

时间:2012-03-27 12:35:03

标签: c# wpf xaml wpf-controls

我有以下依赖项属性,但它不能自动更新。 依赖项属性是RegistrationButton.cls类的所有属性。

public static readonly DependencyProperty DuurStilstandProperty =
        DependencyProperty.Register("DuurStilstand", typeof(string), typeof(RegistrationButton), new UIPropertyMetadata(""));

.NET Wrapper公共属性:

 public string DuurStilstand
    {
        get { return (string)GetValue(DuurStilstandProperty); }
        set { SetValue(DuurStilstandProperty, value); }
    }

我基本上做的是尝试显示一个时间(DateTime默认为3分钟),然后使用计时器使时间不断增加,每秒将日期时间增加1秒。

所以在屏幕上它应该显示00:03:00并开始每秒递增。

我尝试过TwoWay绑定(不确定这是否是问题?)但是当我尝试以下操作时我的应用程序崩溃了:

public static readonly DependencyProperty DuurStilstandProperty =
       DependencyProperty.Register("DuurStilstand", typeof(string), typeof(RegistrationButton), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

涉及创建按钮(显示时间的位置)和计时器的代码:

public void InitializeDispatcherTimerStilstand()
    {
        timerStilstand = new DispatcherTimer();
        timerStilstand.Tick += new EventHandler(timerStilstand_Tick);
        timerStilstand.Interval = new TimeSpan(0, 0, 1);
        timerStilstand.Start();
        timerStilstand.ToString();
    }

    private void timerStilstand_Tick(object sender, EventArgs e)
    {
        this.tijdStilStandRegistrationBtn.AddSeconds(1);
    }

创建按钮的代码:

Btn.Duurstilstand 是保存时间的属性(hh:mm:ss格式)。

tijdStilStandRegistrationBtn = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 3, 0); 

        RegistrationButton btn = new RegistrationButton(GlobalObservableCollection.regBtns.Count.ToString());
        btn.RegistrationCount = GlobalObservableCollection.regBtnCount;
        btn.Title = "btnRegistration" + GlobalObservableCollection.regBtnCount;
        btn.BeginStilstand = btn.Time;
        btn.DuurStilstand = String.Format("{0:T}", this.tijdStilStandRegistrationBtn);

        GlobalObservableCollection.regBtns.Add(btn);
        GlobalObservableCollection.regBtnCount++;

        InitializeDispatcherTimerStilstand();   

目前它似乎只显示00:03:00作为时间,但在我的程序运行时它不会更新。

我也尝试将UpDateSourceTrigger设置为PropertyChanged,但没有区别(它位于RegisterButton类型的样式中)。

<TextBlock x:Name="tbDuurStilstand" TextWrapping="Wrap" 
                                   Text="{Binding DuurStilstand, UpdateSourceTrigger=PropertyChanged}" 
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center" 
                                   Margin="7.5,5,0,0" Height="24.8266666666667"/>

修改

public void CreateRegistrationButton()
    {
        tijdStilStandRegistrationBtn = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 3, 0); 

        RegistrationButton btn = new RegistrationButton(GlobalObservableCollection.regBtns.Count.ToString());
        btn.RegistrationCount = GlobalObservableCollection.regBtnCount;
        btn.Title = "btnRegistration" + GlobalObservableCollection.regBtnCount;
        btn.BeginStilstand = btn.Time;
        //btn.DuurStilstand = String.Format("{0:T}", this.tijdStilStandRegistrationBtn);

        GlobalObservableCollection.regBtns.Add(btn);
        GlobalObservableCollection.regBtnCount++;

        InitializeDispatcherTimerStilstand(btn);     
    }

编辑2:

timerStilstand.Tick += () => timerStilstand_Tick(btn); //error at ()

“委托'System.EventHandler'不接受0参数”

亲切的问候。

1 个答案:

答案 0 :(得分:2)

DateTime是不可变结构。你必须得到结果 我想你需要:

private void timerStilstand_Tick(object sender, EventArgs e)
{
    this.tijdStilStandRegistrationBtn = 
        this.tijdStilStandRegistrationBtn.AddSeconds(1);

    btn.DuurStilstand = 
        String.Format("{0:T}", this.tijdStilStandRegistrationBtn);

}

拿2

public void InitializeDispatcherTimerStilstand(RegistrationButton btn)
{
    timerStilstand = new DispatcherTimer();
    //timerStilstand.Tick += new EventHandler(timerStilstand_Tick);
    timerStilstand.Tick += (aa, bb) => timerStilstand_Tick(btn);   // use capturing to pass btn

    timerStilstand.Interval = new TimeSpan(0, 0, 1);
    timerStilstand.Start();
    //timerStilstand.ToString();
}

private void timerStilstand_Tick(RegistrationButton btn)
{
   this.tijdStilStandRegistrationBtn = 
        this.tijdStilStandRegistrationBtn.AddSeconds(1);

    btn.DuurStilstand = 
        String.Format("{0:T}", this.tijdStilStandRegistrationBtn);
}