using BeDesignerSCADA.Common;
using BeDesignerSCADA.Controls;
using BeDesignerSCADA.View;
using BPASmart.Model;
using BPASmartClient.Compiler;
using BPASmartClient.DATABUS;
using BPASmartClient.MessageName;
using BPASmartClient.MessageName.EnumHelp;
using BPASmartClient.MessageName.发送消息Model;
using BPASmartClient.MessageName.接收消息Model;
using ICSharpCode.AvalonEdit;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
using Microsoft.Win32;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using Xceed.Wpf.Toolkit.PropertyGrid.Attributes;
namespace BeDesignerSCADA.ViewModel
{
public class MainViewModelNew : ObservableObject
{
public MainViewModelNew()
{
IsRunning = false;
//启动 或者 停止
RunUiCommand = new RelayCommand(() =>
{
IsRunning = !IsRunning;
});
}
#region 变量
///
/// 变量管理器地址
///
public string VariablePath = string.Empty;
///
/// 是否正在运行状态
///
private bool _IsRunning = false;
public bool IsRunning
{
get
{
return _IsRunning;
}
set
{
_IsRunning = value;
if (value)
{
RunCanvasVisibility = Visibility.Visible;
CanvasPanelVisibility = Visibility.Hidden;
}
else
{
RunCanvasVisibility = Visibility.Hidden;
CanvasPanelVisibility = Visibility.Visible;
}
OnPropertyChanged("IsRunning");
}
}
///
/// 画布是否显示
///
private Visibility _CanvasPanelVisibility;
public Visibility CanvasPanelVisibility
{
get
{
return _CanvasPanelVisibility;
}
set
{
if (_CanvasPanelVisibility == value)
return;
_CanvasPanelVisibility = value;
OnPropertyChanged("CanvasPanelVisibility");
}
}
///
/// 运行代码是否显示
///
private Visibility _RunCanvasVisibility;
public Visibility RunCanvasVisibility
{
get
{
return _RunCanvasVisibility;
}
set
{
if (_RunCanvasVisibility == value)
return;
_RunCanvasVisibility = value;
OnPropertyChanged("RunCanvasVisibility");
}
}
///
/// 当前代码Xaml
///
private string _XamlCode;
public string XamlCode
{
get
{
return _XamlCode;
}
set
{
if (_XamlCode == value)
return;
_XamlCode = value;
OnPropertyChanged("XamlCode");
}
}
///
/// 选中控件
///
private FrameworkElement _CanSelectedItem;
public FrameworkElement CanSelectedItem
{
get
{
return _CanSelectedItem;
}
set
{
if (_CanSelectedItem == value)
return;
_CanSelectedItem = value;
OnPropertyChanged("CanSelectedItem");
}
}
///
/// 设备名称集合
///
private ObservableCollection _DevNameList;
public ObservableCollection DevNameList
{
get
{
return _DevNameList;
}
set
{
_DevNameList = value;
OnPropertyChanged("DevNameList");
}
}
///
/// 设备变量集合
///
private ObservableCollection _DevValueList;
public ObservableCollection DevValueList
{
get
{
return _DevValueList;
}
set
{
_DevValueList = value;
OnPropertyChanged("DevValueList");
}
}
#endregion
#region 命令
///
/// 启动或者停止
///
public RelayCommand RunUiCommand { get; set; }
///
/// 编辑
///
public RelayCommand EditCommand { get; set; }
#endregion
#region 常用函数
///
/// 显示当前xaml代码
///
public void ShowCode(TextEditor textEditor)
{
textEditor.Text = canvasPanel.Save();
}
#endregion
#region 脚本编辑数据
///
/// 当前编辑界面
///
public CanvasPanel canvasPanel;
///
/// 当前运行界面
///
public RunCanvas runCanvas;
///
/// 加载
///
///
public void Loaded(object objCan, object objRun)
{
canvasPanel = objCan as CanvasPanel;
runCanvas = objRun as RunCanvas;
}
///
/// 编辑
///
///
public void Edit(object obj)
{
string result = JsEditWindow.ShowEdit(((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value?.ToString(), canvasPanel.GetAllExecChildren());
((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = result;
}
///
/// 子控件编辑
///
///
public void ChildEdit(object obj)
{
string xml = ChildEditWindow.ShowEdit(((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value?.ToString());
((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = xml;
}
///
/// 绑定编辑
///
///
public void BingEdit(object obj)
{
//父控件
Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid _property = (Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid)((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).ParentElement;
//选中控件
string controlname = _property.SelectedObjectName;//名称
Type type = _property.SelectedObjectType;//类型
object selectpro = _property.SelectedProperty;
object selectControl= _property.SelectedObject;//对象
string selectProName= _property.SelectedProperty.ToString();
//选中属性
DependencyProperty dependencyProperty = PropertyHelper.GetDependencyProperties(selectControl)?.ToList().Find(x => x.Name == selectProName);
if (dependencyProperty != null)
{
string _binding = PropertyBindingWindow.ShowEdit(dependencyProperty, (selectControl as FrameworkElement));
if (!string.IsNullOrEmpty(_binding))
{
Binding bindingText = new Binding() { Mode = BindingMode.TwoWay, Path = new PropertyPath(_binding) };
(selectControl as FrameworkElement).SetBinding(dependencyProperty, bindingText);
}
else
{
BindingOperations.ClearBinding((selectControl as FrameworkElement), dependencyProperty);
}
}
}
///
/// 清除绑定
///
///
public void ClearBingEdit(object obj)
{
//父控件
Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid _property = (Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid)((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).ParentElement;
//选中控件
string controlname = _property.SelectedObjectName;//名称
Type type = _property.SelectedObjectType;//类型
object selectpro = _property.SelectedProperty;
object selectControl = _property.SelectedObject;//对象
string selectProName = _property.SelectedProperty.ToString();
//选中属性
DependencyProperty dependencyProperty = PropertyHelper.GetDependencyProperties(selectControl)?.ToList().Find(x => x.Name == selectProName);
if (dependencyProperty != null)
{
BindingOperations.ClearBinding((selectControl as FrameworkElement), dependencyProperty);
}
}
///
/// 图片选择路径
///
///
public void SelectPath(object obj)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "图片|*.jpg;*.png;*.gif;*.jpeg;*.bmp";
if (ofd.ShowDialog() == true)
{
((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = ofd.FileName;
}
}
#endregion
}
}