PropertyChangedCallback不更新GUI

时间:2018-09-03 05:19:37

标签: c# wpf

嗨,我有一个mainWindow,它在同一GUI界面下将实时传感器数据传递给usercontrol图表,而PropertyChangedCallback似乎没有更新任何GUI内容(图表未接收任何数据),请在下面找到我的代码:

  1. MainWindow.xaml

    <local:ConstantChangeChart1 x:Name="cscs"></local:ConstantChangeChart1>
    
  2. MainWindow.xaml.cs

    cscs.SetValue(ConstantChangeChart1.MyCustomProperty, main_liveValue_lbl.Content);
    
  3. 用户控件-ConstantChangeChart1

    public static readonly DependencyProperty MyCustomProperty = DependencyProperty.Register("MyCustom", typeof(string), typeof(ConstantChangeChart1),
        new PropertyMetadata(
        new PropertyChangedCallback(OnMyCustomChanged)));
    
    public string MyCustom
    {
        get
        {
            return this.GetValue(MyCustomProperty) as string;
        }
        set
        {
            this.SetValue(MyCustomProperty, value);
            OnPropertyChanged("MyCustom");
        }
    }
    public static void OnMyCustomChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ConstantChangeChart1 originator = d as ConstantChangeChart1;
        if (originator != null)
        {
            originator.OnMyPropertyChanged(d,e);
        }
    }
    
    private void OnMyPropertyChanged(DependencyObject d ,DependencyPropertyChangedEventArgs e)
    {
        ConstantChangeChart1 control = (ConstantChangeChart1)d;
        if(control !=null)
        {
            //Thread.Sleep(150);
            var now = DateTime.Now;
            //_trend += r.Next(-8, 10);
    
            this.Dispatcher.Invoke(() =>
            {
                ChartValues.Add(new Model.MeasureModel
                {
                    DateTime = now,
                    Value = Convert.ToDouble(MyCustom)
                });
            });
    
            SetAxisLimits(now);
    
            //lets only use the last 150 values
            if (ChartValues.Count > 150) ChartValues.RemoveAt(0);
        }
    }
    

0 个答案:

没有答案