@@ -33,11 +33,6 @@ namespace BPASmartClient.SCADAControl.CustomerControls | |||||
public TheDataGrid() | public TheDataGrid() | ||||
{ | { | ||||
InitializeComponent(); | InitializeComponent(); | ||||
//ResourceDictionary languageResDic = new ResourceDictionary(); | |||||
//languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml", UriKind.RelativeOrAbsolute); | |||||
//this.Resources.MergedDictionaries.Add(languageResDic); | |||||
//MinWidth = 100; | |||||
//MinHeight = 100; | |||||
Width = 100; | Width = 100; | ||||
Height = 100; | Height = 100; | ||||
this.Loaded += TheDataGrid_Loaded; ; | this.Loaded += TheDataGrid_Loaded; ; | ||||
@@ -185,7 +180,7 @@ namespace BPASmartClient.SCADAControl.CustomerControls | |||||
set { SetValue(TimeCountProperty,value); } | set { SetValue(TimeCountProperty,value); } | ||||
} | } | ||||
public static readonly DependencyProperty TimeCountProperty = | public static readonly DependencyProperty TimeCountProperty = | ||||
DependencyProperty.Register("TimeCount",typeof(int),typeof(TheDataGrid),new PropertyMetadata(5)); | |||||
DependencyProperty.Register("TimeCount",typeof(int),typeof(TheDataGrid),new PropertyMetadata(500)); | |||||
[Category("数据绑定-数据来源")] | [Category("数据绑定-数据来源")] | ||||
public string DataSouceInformation | public string DataSouceInformation | ||||
{ | { | ||||
@@ -1,6 +1,9 @@ | |||||
using BPASmartClient.Compiler; | using BPASmartClient.Compiler; | ||||
using BPASmartClient.DATABUS; | |||||
using BPASmartClient.MessageName.EnumHelp; | |||||
using BPASmartClient.MessageName.接收消息Model.物料仓; | using BPASmartClient.MessageName.接收消息Model.物料仓; | ||||
using BPASmartClient.SCADAControl.Converters; | using BPASmartClient.SCADAControl.Converters; | ||||
using Newtonsoft.Json; | |||||
using System; | using System; | ||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.Collections.ObjectModel; | using System.Collections.ObjectModel; | ||||
@@ -13,17 +16,20 @@ using System.Windows.Controls; | |||||
using System.Windows.Data; | using System.Windows.Data; | ||||
using System.Windows.Documents; | using System.Windows.Documents; | ||||
using System.Windows.Input; | using System.Windows.Input; | ||||
using System.Windows.Markup; | |||||
using System.Windows.Media; | using System.Windows.Media; | ||||
using System.Windows.Media.Imaging; | using System.Windows.Media.Imaging; | ||||
using System.Windows.Navigation; | using System.Windows.Navigation; | ||||
using System.Windows.Shapes; | using System.Windows.Shapes; | ||||
using System.Windows.Threading; | |||||
using System.Xml; | |||||
namespace BPASmartClient.SCADAControl.CustomerControls | namespace BPASmartClient.SCADAControl.CustomerControls | ||||
{ | { | ||||
/// <summary> | /// <summary> | ||||
/// TheListBox.xaml 的交互逻辑 | /// TheListBox.xaml 的交互逻辑 | ||||
/// </summary> | /// </summary> | ||||
public partial class TheListBox :ListBox, IExecutable | |||||
public partial class TheListBox :ListBox, IExecutable, IDisposable | |||||
{ | { | ||||
public event EventHandler PropertyChange; //声明一个事件 | public event EventHandler PropertyChange; //声明一个事件 | ||||
@@ -33,34 +39,155 @@ namespace BPASmartClient.SCADAControl.CustomerControls | |||||
ResourceDictionary languageResDic = new ResourceDictionary(); | ResourceDictionary languageResDic = new ResourceDictionary(); | ||||
languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml",UriKind.RelativeOrAbsolute); | languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml",UriKind.RelativeOrAbsolute); | ||||
this.Resources.MergedDictionaries.Add(languageResDic); | this.Resources.MergedDictionaries.Add(languageResDic); | ||||
//Style = Application.Current.Resources["DesignTheListBox"] as Style; | |||||
MinWidth = 100; | |||||
MinHeight = 100; | |||||
//ItemsString = new ItemsListObj() | |||||
//{ | |||||
// new datalist { Name="23232",Description="wwewewew",Messgae="564654645"}, | |||||
// new datalist { Name="23232",Description="wwewewew",Messgae="564654645"}, | |||||
// new datalist { Name="23232",Description="wwewewew",Messgae="564654645"}, | |||||
// new datalist { Name="23232",Description="wwewewew",Messgae="564654645"}, | |||||
// new datalist { Name="23232",Description="wwewewew",Messgae="564654645"}, | |||||
// new datalist { Name="23232",Description="wwewewew",Messgae="564654645"}, | |||||
// new datalist { Name="23232",Description="wwewewew",Messgae="564654645"}, | |||||
// new datalist { Name="23232",Description="wwewewew",Messgae="564654645"}, | |||||
// new datalist { Name="23232",Description="wwewewew",Messgae="564654645"}, | |||||
//}; | |||||
EditorHelper.Register<BindingExpression, BindingConvertor>(); | |||||
Width = 100; | |||||
Height = 100; | |||||
} | } | ||||
public ItemsListObj ItemsString | |||||
#region 属性 | |||||
[Category("控件数据集合")] | |||||
private ObservableCollection<object> ItemsString | |||||
{ | { | ||||
get { return (ItemsListObj)GetValue(ItemsStringProperty); } | |||||
set { SetValue(ItemsStringProperty,value); } | |||||
get { return (ObservableCollection<object>)GetValue(ItemsStringProperty); } | |||||
set { SetValue(ItemsStringProperty, value); } | |||||
} | } | ||||
private static readonly DependencyProperty ItemsStringProperty = | |||||
DependencyProperty.Register("ItemsString", typeof(ObservableCollection<object>), typeof(TheListBox), new PropertyMetadata(new ObservableCollection<object>())); | |||||
public static readonly DependencyProperty ItemsStringProperty = | |||||
DependencyProperty.Register("ItemsString",typeof(ItemsListObj),typeof(TheListBox),new PropertyMetadata(null)); | |||||
[Category("子控件模板XML格式")] | |||||
public string ChildTemplateXml | |||||
{ | |||||
get { return (string)GetValue(ChildTemplateXmlProperty); } | |||||
set { SetValue(ChildTemplateXmlProperty, value); } | |||||
} | |||||
public static readonly DependencyProperty ChildTemplateXmlProperty = | |||||
DependencyProperty.Register("ChildTemplateXml", typeof(string), typeof(TheListBox), new PropertyMetadata(string.Empty,new PropertyChangedCallback(onChildTemplateXmlChanged))); | |||||
private static void onChildTemplateXmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheListBox)?.XmlRefresh(); | |||||
public void XmlRefresh() | |||||
{ | |||||
; | |||||
} | |||||
[Category("数据绑定-数据来源")] | |||||
public DataTypeEnum DataSouceType | |||||
{ | |||||
get { return (DataTypeEnum)GetValue(DataSouceTypeProperty); } | |||||
set { SetValue(DataSouceTypeProperty, value); } | |||||
} | |||||
public static readonly DependencyProperty DataSouceTypeProperty = | |||||
DependencyProperty.Register("DataSouceType", typeof(DataTypeEnum), typeof(TheListBox), new PropertyMetadata(DataTypeEnum.API接口)); | |||||
[Category("数据绑定-数据来源")] | |||||
public int TimeCount | |||||
{ | |||||
get { return (int)GetValue(TimeCountProperty); } | |||||
set { SetValue(TimeCountProperty, value); } | |||||
} | |||||
public static readonly DependencyProperty TimeCountProperty = | |||||
DependencyProperty.Register("TimeCount", typeof(int), typeof(TheListBox), new PropertyMetadata(500)); | |||||
[Category("数据绑定-数据来源")] | |||||
public string DataSouceInformation | |||||
{ | |||||
get { return (string)GetValue(DataSouceInformationProperty); } | |||||
set { SetValue(DataSouceInformationProperty, value); } | |||||
} | |||||
public static readonly DependencyProperty DataSouceInformationProperty = | |||||
DependencyProperty.Register("DataSouceInformation", typeof(string), typeof(TheListBox), new PropertyMetadata("api0")); | |||||
[Category("数据绑定")] | |||||
public string FDataSouce | |||||
{ | |||||
get { return (string)GetValue(FDataSouceProperty); } | |||||
set { SetValue(FDataSouceProperty, value); } | |||||
} | |||||
public static readonly DependencyProperty FDataSouceProperty = | |||||
DependencyProperty.Register("FDataSouce", typeof(string), typeof(TheListBox), new PropertyMetadata(string.Empty, new PropertyChangedCallback(onFDataSouceChanged))); | |||||
private static void onFDataSouceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheListBox)?.DataSouceRefresh(); | |||||
public void DataSouceRefresh() | |||||
{ | |||||
try | |||||
{ | |||||
GenerateData = (string)CSharpConfig.GetInstance().RunCSharp(Code, new object[] { FDataSouce }); | |||||
if (PropertyChange != null) | |||||
{ | |||||
PropertyChange(this, null); | |||||
} | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
} | |||||
} | |||||
public static string _code = "public string main(string message) \n{ \n //请在此填写你的代码\n\n return message; \n}\n"; | |||||
[Category("数据绑定")] | |||||
public string Code | |||||
{ | |||||
get { return (string)GetValue(CodeProperty); } | |||||
set { SetValue(CodeProperty, value); } | |||||
} | |||||
public static readonly DependencyProperty CodeProperty = | |||||
DependencyProperty.Register("Code", typeof(string), typeof(TheListBox), new PropertyMetadata(_code)); | |||||
//[Category("数据绑定")] | |||||
//public bool IsRun | |||||
//{ | |||||
// get { return (bool)GetValue(RunProperty); } | |||||
// set { SetValue(RunProperty, value); } | |||||
//} | |||||
//public static readonly DependencyProperty RunProperty = | |||||
// DependencyProperty.Register("IsRun", typeof(bool), typeof(TheListBox), new PropertyMetadata(false, new PropertyChangedCallback(onIsRunChanged))); | |||||
//private static void onIsRunChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheListBox)?.IsRunRefresh(); | |||||
public void IsRunRefresh() | |||||
{ | |||||
////测试运行 | |||||
//if (IsRun) | |||||
// IsExecuteState = true; | |||||
//else | |||||
//{ | |||||
// IsExecuteState = false; | |||||
// Dispose(); | |||||
//} | |||||
} | |||||
[Category("数据绑定")] | |||||
public string GenerateData | |||||
{ | |||||
get { return (string)GetValue(GenerateDataProperty); } | |||||
set { SetValue(GenerateDataProperty, value); } | |||||
} | |||||
public static readonly DependencyProperty GenerateDataProperty = | |||||
DependencyProperty.Register("GenerateData", typeof(string), typeof(TheListBox), new PropertyMetadata(string.Empty, new PropertyChangedCallback(onGenerateDataChanged))); | |||||
private static void onGenerateDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheListBox)?.GenerateDataRefresh(); | |||||
public void GenerateDataRefresh() | |||||
{ | |||||
if (!string.IsNullOrEmpty(GenerateData) && GenerateData.Contains("data")) | |||||
{ | |||||
try | |||||
{ | |||||
Dictionary<string, object> keys = JsonConvert.DeserializeObject<Dictionary<string, object>>(GenerateData); | |||||
ItemsString = JsonConvert.DeserializeObject<ObservableCollection<object>>(keys["data"].ToString()); | |||||
//ItemsString = JsonConvert.DeserializeObject<DataSouceModel>(GenerateData); | |||||
// 运行时进行项目绑定 | |||||
//this.ItemsSource = ListToDataTable(obj2).DefaultView; | |||||
// 运行时进行项目绑定 | |||||
//Binding binding = new Binding(); | |||||
//binding.RelativeSource = new RelativeSource() { Mode = RelativeSourceMode.Self }; | |||||
//binding.Path = new PropertyPath("ItemsString"); | |||||
//SetBinding(ItemsSourceProperty,binding); | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
} | |||||
} | |||||
else | |||||
{ | |||||
//this.Items.Clear(); | |||||
} | |||||
} | |||||
#endregion | |||||
public string ControlType => "控件"; | public string ControlType => "控件"; | ||||
@@ -73,28 +200,162 @@ namespace BPASmartClient.SCADAControl.CustomerControls | |||||
isExecuteState = value; | isExecuteState = value; | ||||
if (IsExecuteState) | if (IsExecuteState) | ||||
{ | { | ||||
//Style = null; | |||||
Register(); | Register(); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
DispatcherTimer timer = new DispatcherTimer(); | |||||
/// <summary> | /// <summary> | ||||
/// 注册需要处理的事件 | /// 注册需要处理的事件 | ||||
/// </summary> | /// </summary> | ||||
public void Register() | public void Register() | ||||
{ | { | ||||
// 运行时进行项目绑定 | |||||
XmlToFrameworkElement(); | |||||
if (DataSouceType == DataTypeEnum.API接口) | |||||
{ | |||||
timer.Interval = TimeSpan.FromMilliseconds(TimeCount); | |||||
timer.Tick += Timer_Tick; ; | |||||
timer.Start(); | |||||
} | |||||
} | |||||
/// <summary> | |||||
/// 流转Xml | |||||
/// </summary> | |||||
/// <param name="framework"></param> | |||||
public void FrameworkElementToXml(FrameworkElement framework) | |||||
{ | |||||
StringBuilder outstr = new StringBuilder(); | |||||
try | |||||
{ | |||||
//this code need for right XML fomating | |||||
XmlWriterSettings settings = new XmlWriterSettings | |||||
{ | |||||
Indent = true, | |||||
OmitXmlDeclaration = true | |||||
}; | |||||
var dsm = new XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings)) | |||||
{ | |||||
//this string need for turning on expression saving mode | |||||
XamlWriterMode = XamlWriterMode.Expression | |||||
}; | |||||
XamlWriter.Save(framework, dsm); | |||||
} | |||||
catch (Exception ex) { } | |||||
ChildTemplateXml = outstr.ToString(); | |||||
} | |||||
public void XmlToFrameworkElement() | |||||
{ | |||||
StackPanel stackPanel = new StackPanel() { Width = 600, Height = 30, Orientation = Orientation.Horizontal }; | |||||
Button button = new Button(); | |||||
button.Name = "button1"; | |||||
Binding bindingButton = new Binding("alarmTime"); | |||||
button.SetBinding(Button.ContentProperty, bindingButton); | |||||
stackPanel.Children.Add(button); | |||||
TextBlock textBlock = new TextBlock(); | |||||
textBlock.Name = "text1"; | |||||
Binding bindingtextBlock = new Binding("alarmMessage"); | |||||
textBlock.SetBinding(TextBlock.TextProperty, bindingtextBlock); | |||||
stackPanel.Children.Add(textBlock); | |||||
FrameworkElementToXml(stackPanel); | |||||
var template = (DataTemplate)XamlReader.Parse(@" | |||||
<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" | |||||
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> | |||||
" + ChildTemplateXml + @" | |||||
</DataTemplate>"); | |||||
SetValue(ItemTemplateProperty, template); | |||||
Binding binding = new Binding(); | Binding binding = new Binding(); | ||||
binding.RelativeSource = new RelativeSource() { Mode = RelativeSourceMode.Self }; | binding.RelativeSource = new RelativeSource() { Mode = RelativeSourceMode.Self }; | ||||
binding.Path = new PropertyPath("ItemsString"); | binding.Path = new PropertyPath("ItemsString"); | ||||
SetBinding(ItemsSourceProperty, binding); | |||||
} | |||||
SetBinding(ItemsSourceProperty,binding); | |||||
public void Dispose() | |||||
{ | |||||
timer.Stop(); | |||||
FDataSouce = ""; | |||||
//if (IsRun) IsRun = false; | |||||
GenerateDataRefresh(); | |||||
} | } | ||||
private void MyButton_Click(object sender,RoutedEventArgs e) | |||||
private void Timer_Tick(object? sender, EventArgs e) | |||||
{ | { | ||||
if (!string.IsNullOrEmpty(DataSouceInformation)) | |||||
{ | |||||
if (DataSouceType == DataTypeEnum.API接口) | |||||
{ | |||||
if (Class_DataBus.GetInstance().Dic_APIData.ContainsKey(DataSouceInformation)) | |||||
{ | |||||
FDataSouce = Class_DataBus.GetInstance().Dic_APIData[DataSouceInformation]; | |||||
} | |||||
} | |||||
else if (DataSouceType == DataTypeEnum.Redis) | |||||
{ | |||||
if (DataSouceInformation.Contains(".") && DataSouceInformation.Contains("{Binding ")) | |||||
{ | |||||
string[] str = DataSouceInformation.Replace("{Binding ", "").Replace("}", "").Split("."); | |||||
if (str.Length > 1) | |||||
{ | |||||
if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0])) | |||||
{ | |||||
Dictionary<string, DeviceDataModel> b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; | |||||
if (b != null && b.ContainsKey(str[1])) | |||||
{ | |||||
FDataSouce = b[str[1]].VarVaule; | |||||
} | |||||
} | |||||
} | |||||
} | |||||
} | |||||
else if (DataSouceType == DataTypeEnum.静态数据) | |||||
{ | |||||
FDataSouce = ""; | |||||
} | |||||
} | |||||
} | |||||
} | |||||
internal class BindingConvertor : ExpressionConverter | |||||
{ | |||||
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) | |||||
{ | |||||
if (destinationType == typeof(MarkupExtension)) | |||||
return true; | |||||
else return false; | |||||
} | |||||
public override object ConvertTo(ITypeDescriptorContext context, | |||||
System.Globalization.CultureInfo culture, object value, Type destinationType) | |||||
{ | |||||
if (destinationType == typeof(MarkupExtension)) | |||||
{ | |||||
BindingExpression bindingExpression = value as BindingExpression; | |||||
if (bindingExpression == null) | |||||
throw new Exception(); | |||||
return bindingExpression.ParentBinding; | |||||
} | |||||
return base.ConvertTo(context, culture, value, destinationType); | |||||
} | |||||
} | |||||
internal static class EditorHelper | |||||
{ | |||||
public static void Register<T, TC>() | |||||
{ | |||||
Attribute[] attr = new Attribute[1]; | |||||
TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC)); | |||||
attr[0] = vConv; | |||||
TypeDescriptor.AddAttributes(typeof(T), attr); | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -269,6 +269,9 @@ | |||||
<mypro:PropertyDefinition DisplayName="列表项" Category="集合设置" Name="ItemsString" /> | <mypro:PropertyDefinition DisplayName="列表项" Category="集合设置" Name="ItemsString" /> | ||||
<mypro:PropertyDefinition DisplayName="列表项" Category="集合设置" Name="TabItems" /> | <mypro:PropertyDefinition DisplayName="列表项" Category="集合设置" Name="TabItems" /> | ||||
<mypro:PropertyDefinition DisplayName="子控件模板" Category="集合设置" Name="ChildTemplateXml" /> | |||||
<mypro:PropertyDefinition DisplayName="前景色" Category="颜色设置" Name="Foreground"/> | <mypro:PropertyDefinition DisplayName="前景色" Category="颜色设置" Name="Foreground"/> | ||||
<mypro:PropertyDefinition DisplayName="背景色" Category="颜色设置" Name="Background"/> | <mypro:PropertyDefinition DisplayName="背景色" Category="颜色设置" Name="Background"/> | ||||
@@ -316,6 +319,19 @@ | |||||
</DataTemplate> | </DataTemplate> | ||||
</mypro:EditorTemplateDefinition.EditingTemplate> | </mypro:EditorTemplateDefinition.EditingTemplate> | ||||
</mypro:EditorTemplateDefinition> | </mypro:EditorTemplateDefinition> | ||||
<!--子控件模板编辑--> | |||||
<mypro:EditorTemplateDefinition TargetProperties="ChildTemplateXml"> | |||||
<mypro:EditorTemplateDefinition.EditingTemplate> | |||||
<DataTemplate> | |||||
<Grid> | |||||
<ToggleButton Grid.Column="1" Height="30" Content=" 设置控件模板 " Margin="5,0,0,0" Click="ChildToggleButton_Click"></ToggleButton> | |||||
<TextBox Text="{Binding Value,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" Visibility="Collapsed"/> | |||||
</Grid> | |||||
</DataTemplate> | |||||
</mypro:EditorTemplateDefinition.EditingTemplate> | |||||
</mypro:EditorTemplateDefinition> | |||||
<!--代码绑定模块--> | <!--代码绑定模块--> | ||||
<mypro:EditorTemplateDefinition TargetProperties="ClickExec,ValueChangedExecute,TikcExecute,CheckedExec,UnCheckedExec" > | <mypro:EditorTemplateDefinition TargetProperties="ClickExec,ValueChangedExecute,TikcExecute,CheckedExec,UnCheckedExec" > | ||||
@@ -383,8 +399,14 @@ | |||||
<ComboBox x:Name="valuebox" DropDownOpened="valuebox_DropDownOpened" Grid.Column="2" Height="25" IsTextSearchEnabled="True" IsEditable="True" Tag="{Binding Text, ElementName=namebox}" TextBoxBase.TextChanged="ComboBoxValue_TextChanged" | <ComboBox x:Name="valuebox" DropDownOpened="valuebox_DropDownOpened" Grid.Column="2" Height="25" IsTextSearchEnabled="True" IsEditable="True" Tag="{Binding Text, ElementName=namebox}" TextBoxBase.TextChanged="ComboBoxValue_TextChanged" | ||||
ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl},Path=DataContext.DevValueList}"/> | ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl},Path=DataContext.DevValueList}"/> | ||||
</Grid> | </Grid> | ||||
<TextBox Grid.Row="2" x:Name="wenben" Padding="5" Text="{Binding Value,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap"></TextBox> | |||||
<Grid Grid.Row="2"> | |||||
<Grid.ColumnDefinitions> | |||||
<ColumnDefinition Width="*"/> | |||||
<ColumnDefinition Width="40"/> | |||||
</Grid.ColumnDefinitions> | |||||
<TextBox Grid.Row="2" x:Name="wenben" Padding="5" Text="{Binding Value,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap"></TextBox> | |||||
<ToggleButton Grid.Column="1" Height="20" Content="绑定" Width="30" Margin="5,0,5,0" HorizontalAlignment="Left" Click="BingToggleButton_Click"></ToggleButton> | |||||
</Grid> | |||||
</Grid> | </Grid> | ||||
</DataTemplate> | </DataTemplate> | ||||
</mypro:EditorTemplateDefinition.EditingTemplate> | </mypro:EditorTemplateDefinition.EditingTemplate> | ||||
@@ -6,6 +6,7 @@ using BPASmartClient.DATABUS; | |||||
using BPASmartClient.MessageName; | using BPASmartClient.MessageName; | ||||
using System; | using System; | ||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.ComponentModel; | |||||
using System.IO; | using System.IO; | ||||
using System.Linq; | using System.Linq; | ||||
using System.Reflection; | using System.Reflection; | ||||
@@ -18,6 +19,7 @@ using System.Windows.Data; | |||||
using System.Windows.Documents; | using System.Windows.Documents; | ||||
using System.Windows.Forms; | using System.Windows.Forms; | ||||
using System.Windows.Input; | using System.Windows.Input; | ||||
using System.Windows.Markup; | |||||
using System.Windows.Media; | using System.Windows.Media; | ||||
using System.Windows.Media.Animation; | using System.Windows.Media.Animation; | ||||
using System.Windows.Media.Imaging; | using System.Windows.Media.Imaging; | ||||
@@ -40,7 +42,8 @@ namespace BeDesignerSCADA.Controls | |||||
public MainCanvasPanel(string _Path) | public MainCanvasPanel(string _Path) | ||||
{ | { | ||||
InitializeComponent(); | InitializeComponent(); | ||||
Path= _Path; | |||||
EditorHelper.Register<BindingExpression, BindingConvertor>(); | |||||
Path = _Path; | |||||
this.DataContext = viewModel; | this.DataContext = viewModel; | ||||
viewModel.Loaded(cav, runCanvas); | viewModel.Loaded(cav, runCanvas); | ||||
//控件加载 | //控件加载 | ||||
@@ -200,6 +203,44 @@ namespace BeDesignerSCADA.Controls | |||||
} | } | ||||
} | } | ||||
/// <summary> | |||||
/// 根据流加载数据 | |||||
/// </summary> | |||||
public void LoadFrameworkElement(UIElementCollection frameworks) | |||||
{ | |||||
try | |||||
{ | |||||
cav.Children.Clear(); | |||||
foreach (FrameworkElement element in frameworks) | |||||
{ | |||||
string xamlText = XamlWriter.Save(element); | |||||
FrameworkElement item = XamlReader.Parse(xamlText) as FrameworkElement; | |||||
cav.Children.Add(item); | |||||
} | |||||
cav.SelectedItems?.Clear(); | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
throw; | |||||
} | |||||
} | |||||
/// <summary> | |||||
/// 获取所有控件 | |||||
/// </summary> | |||||
/// <returns></returns> | |||||
public List<FrameworkElement> GetChildren() | |||||
{ | |||||
List<FrameworkElement> frameworks = new List<FrameworkElement>(); | |||||
foreach (FrameworkElement child in cav.Children) | |||||
{ | |||||
string xamlText = XamlWriter.Save(child); | |||||
FrameworkElement item = XamlReader.Parse(xamlText) as FrameworkElement; | |||||
frameworks.Add(item); | |||||
} | |||||
return frameworks; | |||||
} | |||||
/// <summary> | /// <summary> | ||||
/// 保存文件 | /// 保存文件 | ||||
/// </summary> | /// </summary> | ||||
@@ -359,6 +400,55 @@ namespace BeDesignerSCADA.Controls | |||||
} | } | ||||
} | } | ||||
/// <summary> | /// <summary> | ||||
/// 设置子控件模板 | |||||
/// </summary> | |||||
/// <param name="sender"></param> | |||||
/// <param name="e"></param> | |||||
private void ChildToggleButton_Click(object sender, RoutedEventArgs e) | |||||
{ | |||||
try | |||||
{ | |||||
if (sender is ToggleButton) | |||||
{ | |||||
ToggleButton toggle = (ToggleButton)sender; | |||||
Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem; | |||||
if (propertyGridCommand != null) | |||||
{ | |||||
viewModel.ChildEdit(propertyGridCommand); | |||||
} | |||||
} | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
} | |||||
} | |||||
/// <summary> | |||||
/// 数据绑定 | |||||
/// </summary> | |||||
/// <param name="sender"></param> | |||||
/// <param name="e"></param> | |||||
private void BingToggleButton_Click(object sender, RoutedEventArgs e) | |||||
{ | |||||
try | |||||
{ | |||||
if (sender is ToggleButton) | |||||
{ | |||||
ToggleButton toggle = (ToggleButton)sender; | |||||
Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem; | |||||
if (propertyGridCommand != null) | |||||
{ | |||||
viewModel.BingEdit(propertyGridCommand); | |||||
} | |||||
} | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
} | |||||
} | |||||
/// <summary> | |||||
/// 变量选择 | /// 变量选择 | ||||
/// </summary> | /// </summary> | ||||
/// <param name="sender"></param> | /// <param name="sender"></param> | ||||
@@ -453,6 +543,43 @@ namespace BeDesignerSCADA.Controls | |||||
} | } | ||||
communication?.CommunicationDevices?.ToList().ForEach(x => { viewModel.DevNameList.Add(x.DeviceName); }); | communication?.CommunicationDevices?.ToList().ForEach(x => { viewModel.DevNameList.Add(x.DeviceName); }); | ||||
} | } | ||||
#endregion | #endregion | ||||
} | |||||
internal class BindingConvertor : ExpressionConverter | |||||
{ | |||||
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) | |||||
{ | |||||
if (destinationType == typeof(MarkupExtension)) | |||||
return true; | |||||
else return false; | |||||
} | |||||
public override object ConvertTo(ITypeDescriptorContext context, | |||||
System.Globalization.CultureInfo culture, object value, Type destinationType) | |||||
{ | |||||
if (destinationType == typeof(MarkupExtension)) | |||||
{ | |||||
BindingExpression bindingExpression = value as BindingExpression; | |||||
if (bindingExpression == null) | |||||
throw new Exception(); | |||||
return bindingExpression.ParentBinding; | |||||
} | |||||
return base.ConvertTo(context, culture, value, destinationType); | |||||
} | |||||
} | |||||
internal static class EditorHelper | |||||
{ | |||||
public static void Register<T, TC>() | |||||
{ | |||||
Attribute[] attr = new Attribute[1]; | |||||
TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC)); | |||||
attr[0] = vConv; | |||||
TypeDescriptor.AddAttributes(typeof(T), attr); | |||||
} | |||||
} | } | ||||
} | } |
@@ -0,0 +1,38 @@ | |||||
<Window x:Class="BeDesignerSCADA.View.ChildEditWindow" | |||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |||||
xmlns:local="clr-namespace:BeDesignerSCADA.View" | |||||
mc:Ignorable="d" | |||||
Title="子控件编辑器" Height="800" Width="920" | |||||
Background="White" | |||||
BorderBrush="#FFDEDEDE" | |||||
BorderThickness="1" | |||||
WindowState="Maximized" | |||||
WindowStartupLocation="CenterOwner"> | |||||
<Window.Resources> | |||||
<ResourceDictionary> | |||||
<ResourceDictionary.MergedDictionaries> | |||||
<ResourceDictionary Source="/BeDesignerSCADA;component/Themes/Styles.xaml" /> | |||||
<ResourceDictionary Source="/BPASmartClient.SCADAControl;component/Themes/Generic.xaml" /> | |||||
</ResourceDictionary.MergedDictionaries> | |||||
</ResourceDictionary> | |||||
</Window.Resources> | |||||
<Grid> | |||||
<Grid.RowDefinitions> | |||||
<RowDefinition/> | |||||
<RowDefinition Height="40"/> | |||||
</Grid.RowDefinitions> | |||||
<Border x:Name="main"> | |||||
</Border> | |||||
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right"> | |||||
<Button Width="80" Height="30" Content="取消" x:Name="CancelBtn" Click="CancelBtn_Click" IsCancel="True"/> | |||||
<Button Width="80" Height="30" Content="确认" x:Name="ConfirmBtn" Margin="8 0 4 0" Click="ConfirmBtn_Click"/> | |||||
</StackPanel> | |||||
</Grid> | |||||
</Window> |
@@ -0,0 +1,111 @@ | |||||
using BeDesignerSCADA.Controls; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Text; | |||||
using System.Threading.Tasks; | |||||
using System.Windows; | |||||
using System.Windows.Controls; | |||||
using System.Windows.Data; | |||||
using System.Windows.Documents; | |||||
using System.Windows.Input; | |||||
using System.Windows.Markup; | |||||
using System.Windows.Media; | |||||
using System.Windows.Media.Imaging; | |||||
using System.Windows.Shapes; | |||||
using System.Xml; | |||||
namespace BeDesignerSCADA.View | |||||
{ | |||||
/// <summary> | |||||
/// ChildEditWindow.xaml 的交互逻辑 | |||||
/// </summary> | |||||
public partial class ChildEditWindow : Window | |||||
{ | |||||
public ChildEditWindow() | |||||
{ | |||||
InitializeComponent(); | |||||
EditorHelper.Register<BindingExpression, BindingConvertor>(); | |||||
Owner = Application.Current.MainWindow; | |||||
Closing += (s, e) => { e.Cancel = true; Hide(); }; | |||||
} | |||||
public static ChildEditWindow Instance { get; } = new ChildEditWindow(); | |||||
public static string ShowEdit(string xml) | |||||
{ | |||||
try | |||||
{ | |||||
// 获取所有控件的可用属性 | |||||
Instance.main.Child = new MainCanvasPanel(""); | |||||
if (!string.IsNullOrEmpty(xml)) | |||||
{ | |||||
FrameworkElement element = XamlReader.Parse(xml) as FrameworkElement; | |||||
if (element != null && element is Canvas) | |||||
{ | |||||
(Instance.main.Child as MainCanvasPanel).LoadFrameworkElement(((element as Canvas).Children)); | |||||
} | |||||
} | |||||
Instance.ShowDialog(); | |||||
if (Instance.IsOk) | |||||
{ | |||||
Canvas grid =new Canvas(); | |||||
(Instance.main.Child as MainCanvasPanel).GetChildren()?.ForEach(child => { | |||||
grid.Children.Add(child); | |||||
}); | |||||
return FrameworkElementToXml(grid); | |||||
} | |||||
else | |||||
{ | |||||
return xml; | |||||
} | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
return xml; | |||||
} | |||||
} | |||||
/// <summary> | |||||
/// 流转Xml | |||||
/// </summary> | |||||
/// <param name="framework"></param> | |||||
public static string FrameworkElementToXml(FrameworkElement framework) | |||||
{ | |||||
StringBuilder outstr = new StringBuilder(); | |||||
try | |||||
{ | |||||
//this code need for right XML fomating | |||||
XmlWriterSettings settings = new XmlWriterSettings | |||||
{ | |||||
Indent = true, | |||||
OmitXmlDeclaration = true | |||||
}; | |||||
var dsm = new XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings)) | |||||
{ | |||||
//this string need for turning on expression saving mode | |||||
XamlWriterMode = XamlWriterMode.Expression | |||||
}; | |||||
XamlWriter.Save(framework, dsm); | |||||
} | |||||
catch (Exception ex) { } | |||||
return outstr.ToString(); | |||||
} | |||||
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(); | |||||
} | |||||
} | |||||
} |
@@ -11,8 +11,7 @@ | |||||
BorderBrush="#FFDEDEDE" | BorderBrush="#FFDEDEDE" | ||||
BorderThickness="1" | BorderThickness="1" | ||||
ResizeMode="NoResize" | ResizeMode="NoResize" | ||||
WindowStartupLocation="CenterOwner" | |||||
WindowStyle="None"> | |||||
WindowStartupLocation="CenterOwner"> | |||||
<Grid> | <Grid> | ||||
<Grid.RowDefinitions> | <Grid.RowDefinitions> | ||||
<RowDefinition/> | <RowDefinition/> | ||||
@@ -37,8 +37,6 @@ namespace BeDesignerSCADA.ViewModel | |||||
{ | { | ||||
IsRunning = !IsRunning; | IsRunning = !IsRunning; | ||||
}); | }); | ||||
//DevNameList = new ObservableCollection<string>() { "qqqq", "wewew" }; | |||||
//DataBusModel.GetInstance().RefreshTheVariableManagerAction += RefreshTheVariableManagerActionHader; | |||||
} | } | ||||
#region 变量 | #region 变量 | ||||
@@ -46,10 +44,7 @@ namespace BeDesignerSCADA.ViewModel | |||||
/// 变量管理器地址 | /// 变量管理器地址 | ||||
/// </summary> | /// </summary> | ||||
public string VariablePath = string.Empty; | public string VariablePath = string.Empty; | ||||
/// <summary> | |||||
/// 当前变量管理器变量 | |||||
/// </summary> | |||||
public CommunicationPar communicationPar = new CommunicationPar(); | |||||
/// <summary> | /// <summary> | ||||
/// 是否正在运行状态 | /// 是否正在运行状态 | ||||
/// </summary> | /// </summary> | ||||
@@ -207,27 +202,6 @@ namespace BeDesignerSCADA.ViewModel | |||||
{ | { | ||||
textEditor.Text = canvasPanel.Save(); | textEditor.Text = canvasPanel.Save(); | ||||
} | } | ||||
/// <summary> | |||||
/// 刷新变量 | |||||
/// </summary> | |||||
/// <param name="path"></param> | |||||
public void RefreshTheVariableManagerActionHader(string path) | |||||
{ | |||||
try | |||||
{ | |||||
//if (path == VariablePath && !string.IsNullOrEmpty(VariablePath)) | |||||
//{ | |||||
// if (DataBusModel.GetInstance().KeyValues.ContainsKey(VariablePath)) | |||||
// { | |||||
// communicationPar = DataBusModel.GetInstance().KeyValues[VariablePath]; | |||||
// } | |||||
//} | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
} | |||||
} | |||||
#endregion | #endregion | ||||
#region 脚本编辑数据 | #region 脚本编辑数据 | ||||
@@ -258,6 +232,31 @@ namespace BeDesignerSCADA.ViewModel | |||||
string result = JsEditWindow.ShowEdit(((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value?.ToString(), canvasPanel.GetAllExecChildren()); | string result = JsEditWindow.ShowEdit(((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value?.ToString(), canvasPanel.GetAllExecChildren()); | ||||
((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = result; | ((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = result; | ||||
} | } | ||||
/// <summary> | |||||
/// 子控件编辑 | |||||
/// </summary> | |||||
/// <param name="obj"></param> | |||||
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; | |||||
} | |||||
/// <summary> | |||||
/// 绑定编辑 | |||||
/// </summary> | |||||
/// <param name="obj"></param> | |||||
public void BingEdit(object obj) | |||||
{ | |||||
//System.Windows.Data.Binding bindingtextBlock = new System.Windows.Data.Binding("alarmMessage"); | |||||
//textBlock.SetBinding(TextBlock.TextProperty, bindingtextBlock); | |||||
// ((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).SetBinding(((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value, "name"); | |||||
//string xml = ChildEditWindow.ShowEdit(((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value?.ToString()); | |||||
//((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = xml; | |||||
} | |||||
/// <summary> | /// <summary> | ||||
/// 图片选择路径 | /// 图片选择路径 | ||||
/// </summary> | /// </summary> | ||||
@@ -271,6 +270,8 @@ namespace BeDesignerSCADA.ViewModel | |||||
((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = ofd.FileName; | ((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = ofd.FileName; | ||||
} | } | ||||
} | } | ||||
#endregion | #endregion | ||||
} | } | ||||
} | } |