首页 > 代码库 > [开源]FreeSCADA的通道数据与控件属性关联以及自动刷新机制研究
[开源]FreeSCADA的通道数据与控件属性关联以及自动刷新机制研究
1. 几个重要的类列表:
a) Designer工程下的CommonBindingDialog.cs:
b) Designer工程下的NumericBindingPanel.cs(或者StringBindingPanel.cs):
2. 通道数据与控件属性关联(以及自动刷新机制):
a) 通道数据与控件属性绑定:
CommonBindingDialog.cs中的事件响应方法(Create association按钮被按下):
private void CreateAssociationButton_Click(object sender, EventArgs e){ SavePanelStateAndClose(); if (propertyList.SelectedIndex >= 0 && bindingTypes.SelectedIndex >= 0) { BaseBindingPanelFactory factory = (BaseBindingPanelFactory)bindingTypes.SelectedItem; // factory在这里执行过后,就是NumericBindingPanel.cs里定义的NumericBindingPanelFactory类型了 bindingPanel = factory.CreateInstance(); // bindingPanel的具体类型是NumericBindingPanel还是StringBindingPanel,在这里得到了重新定义(不再是基类的BaseBindingPanel类型了) bindingPanel.Initialize(element, propertyList.SelectedItem as PropertyWrapper, null); bindingPanel.Parent = panel1; bindingPanel.Dock = DockStyle.Fill; CreateAssociationButton.Enabled = false; bindingTypes.Enabled = false; }}
CommonBindingDialog.cs中的事件响应方法(Save按钮被按下):
private void saveButton_Click(object sender, EventArgs e){ SavePanelStateAndClose(); if (activeBindings.Count > 0) { foreach (PropertyWrapper key in activeBindings.Keys) { DependencyObject depObj; DependencyProperty depProp; System.Windows.Data.BindingBase binding = activeBindings[key]; if (key.GetWpfObjects(out depObj, out depProp) && binding != null) BindingOperations.SetBinding(depObj, depProp, binding); } } DialogResult = DialogResult.OK; Close();}
继续来看SavePanelStateAndClose方法的实现:
private void SavePanelStateAndClose(){ if (bindingPanel != null) { BindingBase binding = bindingPanel.Save(); // 见NumericBindingPanel类的Save()定义 if (binding != null) activeBindings[bindingPanel.Property] = binding; // activeBindings为Dictionary<PropertyWrapper, BindingBase>,bindingPanel.Property为PropertyWrapper bindingPanel.Dispose(); bindingPanel = null; }}
我们再继续深入NumericBindingPanel.cs里面定义的Save方法:
public override System.Windows.Data.BindingBase Save(){ if (channel != null) { System.Windows.Data.Binding bind = new System.Windows.Data.Binding("Value"); ChannelDataProvider cdp = new ChannelDataProvider(); cdp.ChannelName = channel.PluginId + "." + channel.Name; bind.Source = cdp; // 绑定的源数据为通道数据 bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; cdp.Refresh(); ComposingConverter conv = new ComposingConverter(); if (checkBox1.Checked) { RangeConverter rc = new RangeConverter(); rc.Min = Decimal.ToDouble(minEdit.Value); rc.Max = Decimal.ToDouble(maxEdit.Value); conv.Converters.Add(rc); } conv.Converters.Add(new Kent.Boogaart.Converters.TypeConverter(cdp.Channel.Type, Property.PropertyType)); bind.Converter = conv; bind.Mode = BindingMode.TwoWay; DependencyObject depObj; DependencyProperty depProp; if (Property.GetWpfObjects(out depObj, out depProp)) bind.FallbackValue = depObj.GetValue(depProp); return bind; } else return base.Save();}
[开源]FreeSCADA的通道数据与控件属性关联以及自动刷新机制研究
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。