c# - PropertyChangedCallback 不更新 GUI
问题描述
嗨,我有一个 mainWindow,它在同一个 GUI 界面下将实时传感器数据传递给 usercontrol 图表,而 PropertyChangedCallback 似乎没有更新任何 GUI 内容(图表没有收到任何数据),请在下面找到我的代码:
主窗口.xaml
<local:ConstantChangeChart1 x:Name="cscs"></local:ConstantChangeChart1>
主窗口.xaml.cs
cscs.SetValue(ConstantChangeChart1.MyCustomProperty, main_liveValue_lbl.Content);
用户控件-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# - 如何使用数据网格添加某个值来更新数据库?
- mysql - mysql计算前行时间戳差异并按多个对象分组
- php - Angular 4/5 中 php getallheaders() 函数的等价物是什么?
- perl - TAP::Harness::JUnit 不会将输出刷新到 STDOUT
- ios - 如何设置设置以获取以 kb 为单位的录制音频文件大小?
- python - 我想通过 Gmail 发送文件,但遇到了问题
- javascript - bootstrap 4插件-它们在jquery中注册的方法
- android - Chromium for Android:如何始终保持全屏模式
- image - 裁剪和旋转捕获的图片 Xamarin.Android 和 Xamarin.iOS
- google-cloud-platform - Kubernetes Engine 无法从非私有/GCR 存储库中提取映像