如何将静态对象发送到`PropertyChanged`的事件中

时间:2012-04-30 11:33:06

标签: wpf mvvm user-controls static dependency-properties

我有MVVM模式的项目。我也有一个用户控件。此用户Control具有静态DependencyProperty

public partial class RadarView : INotifyPropertyChanged
{


public static DispatcherTimer Timer { get; set; }

        public static readonly DependencyProperty RequestTypeProperty = DependencyProperty.Register("RequestType", typeof(RadarRequestType), typeof(RadarView), new FrameworkPropertyMetadata(new RadarRequestType(), RequestTypeChanged));

        public RadarRequestType RequestType
        {
            get
            {
                return
                    (RadarRequestType)GetValue(RequestTypeProperty);
            }
            set { SetValue(RequestTypeProperty, value); }
        }

        public static void RequestTypeChanged(DependencyObject dobject, DependencyPropertyChangedEventArgs args)
        {
        var radar = (RadarView)dobject;

            if (Timer == null)
                Timer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 1) };

            Timer.Tick += DispatcherTimerTick;

            Timer.Start();

        }

        private static void DispatcherTimerTick(object sender, EventArgs e)
        {
            StartText = DateTime.Now.Second.ToString();

            PropertyChanged(this, new PropertyChangedEventArgs("StartText"));
        }

        public static event PropertyChangedEventHandler PropertyChanged = delegate { };

}

我的问题出现在DispatcherTimerTick中,因为它在it事件中与我的用户Control相同,并且它不是静态的。此构建错误的消息是:关键字'this'在静态属性,静态方法或静态字段初始值设定项中无效

实际上我需要将(RadarView)dobject发送到DispatcherTimerTick事件。我该怎么办?

1 个答案:

答案 0 :(得分:0)

我的问题解决了。我对Timer的使用是错误的。我需要radar.Timer