首页 > 解决方案 > PropertyChangedCallback 不更新 GUI

问题描述

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

  1. 主窗口.xaml

    <local:ConstantChangeChart1 x:Name="cscs"></local:ConstantChangeChart1>
    
  2. 主窗口.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);
        }
    }
    

标签: c#wpf

解决方案


推荐阅读