using BeDesignerSCADA.Common;
using BeDesignerSCADA.ViewModel;
using BPASmartClient.SCADAControl;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace BeDesignerSCADA.View
{
///
/// PropertyBindingWindow.xaml 的交互逻辑
///
public partial class PropertyBindingWindow : Window
{
public PropertyBindingWindow()
{
InitializeComponent();
Owner = Application.Current.MainWindow;
Closing += (s, e) => { e.Cancel = true; Hide(); };
}
public static PropertyBindingWindow Instance { get; } = new PropertyBindingWindow();
///
/// 设备名称集合
///
private ObservableCollection _DevNameList;
public ObservableCollection DevNameList
{
get
{
return _DevNameList;
}
set
{
_DevNameList = value;
}
}
///
/// 设备变量集合
///
private ObservableCollection _DevValueList;
public ObservableCollection DevValueList
{
get
{
return _DevValueList;
}
set
{
_DevValueList = value;
}
}
public string VariablePath = string.Empty;
public static string ShowEdit(DependencyProperty selectproperty, FrameworkElement control, string _VariablePath)
{
// 获取所有控件的可用属性
var pros = PropertyHelper.GetBindString(selectproperty, control);
Instance.VariablePath = _VariablePath;
Instance.ProperpName.Text = selectproperty.Name;
Instance.BindingName.Text = pros;
Instance.ShowDialog();
if (Instance.IsOk)
{
return Instance.BindingName.Text;
}
else
{
return pros;
}
}
public bool IsOk { get; set; }
private void CancelBtn_Click(object sender, RoutedEventArgs e)
{
IsOk = false;
Close();
}
private void ConfirmBtn_Click(object sender, RoutedEventArgs e)
{
IsOk = true;
Close();
}
private void ClearBtn_Click(object sender, RoutedEventArgs e)
{
Instance.BindingName.Text = "";
IsOk = true;
Close();
}
///
/// 变量选择
///
///
///
private void ComboBoxValue_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
if (sender is System.Windows.Controls.ComboBox)
{
System.Windows.Controls.ComboBox toggle = (System.Windows.Controls.ComboBox)sender;
if (toggle.Tag != null && !string.IsNullOrEmpty(toggle.Text))
{
Instance.BindingName.Text = string.Format("DataModel[{0}].{1}", toggle.Tag, toggle.Text);
}
}
}
catch (Exception ex)
{
}
}
///
/// 变量下拉框打开事件
///
///
///
private void valuebox_DropDownOpened(object sender, EventArgs e)
{
try
{
Instance.DevValueList = new System.Collections.ObjectModel.ObservableCollection();
if (sender is System.Windows.Controls.ComboBox)
{
System.Windows.Controls.ComboBox toggle = (System.Windows.Controls.ComboBox)sender;
if (toggle.Tag == null) return;
CommunicationPar communication = null;
if (DataBusModel.GetInstance().KeyValues.ContainsKey(Instance.VariablePath))
{
communication = DataBusModel.GetInstance().KeyValues[Instance.VariablePath];
}
CommunicationModel mode = communication?.CommunicationDevices?.ToList().Find(par => par.DeviceName == toggle.Tag.ToString());
if (mode != null)
{
mode?.VarTableModels?.ToList().ForEach(par => { Instance.DevValueList.Add(par.VarName); });
}
}
valuebox.ItemsSource = Instance.DevValueList;
}
catch (Exception ex)
{
}
}
///
/// 设备名称选择
///
///
///
private void ComboBoxName_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
if (sender is System.Windows.Controls.ComboBox)
{
System.Windows.Controls.ComboBox toggle = (System.Windows.Controls.ComboBox)sender;
if (toggle.Tag != null && !string.IsNullOrEmpty(toggle.Text))
{
Instance.BindingName.Text = string.Format("DataModel[{0}].{1}", toggle.Text, toggle.Tag);
}
}
}
catch (Exception ex)
{
}
}
///
/// 设备名称下拉框打开事件
///
///
///
private void namebox_DropDownOpened(object sender, EventArgs e)
{
Instance.DevNameList = new System.Collections.ObjectModel.ObservableCollection();
CommunicationPar communication = null;
if (DataBusModel.GetInstance().KeyValues.ContainsKey(Instance.VariablePath))
{
communication = DataBusModel.GetInstance().KeyValues[Instance.VariablePath];
}
communication?.CommunicationDevices?.ToList().ForEach(x => { Instance.DevNameList.Add(x.DeviceName); });
namebox.ItemsSource = Instance.DevNameList;
}
}
}