ソースを参照

更新绑定模块

master
fyf 2年前
コミット
fc7fbab476
7個のファイルの変更285行の追加44行の削除
  1. +83
    -2
      BeDesignerSCADA/Common/PropertyHelper.cs
  2. +1
    -0
      BeDesignerSCADA/Controls/MainCanvasPanel.xaml
  3. +29
    -34
      BeDesignerSCADA/Controls/MainCanvasPanel.xaml.cs
  4. +2
    -1
      BeDesignerSCADA/View/ChildEditWindow.xaml.cs
  5. +43
    -0
      BeDesignerSCADA/View/PropertyBindingWindow.xaml
  6. +71
    -0
      BeDesignerSCADA/View/PropertyBindingWindow.xaml.cs
  7. +56
    -7
      BeDesignerSCADA/ViewModel/MainViewModelNew.cs

+ 83
- 2
BeDesignerSCADA/Common/PropertyHelper.cs ファイルの表示

@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;

namespace BeDesignerSCADA.Common
{
@@ -13,8 +16,11 @@ namespace BeDesignerSCADA.Common
public class PropertyHelper
{
public static string[] ableProperties = new string[]
{ "IsChecked", "Value", "CurValue", "StatusValue", "NumberValue", "Text",
"Direction","RefreshData","ChangedText","Content","SendText"
{
"IsChecked", "Value", "CurValue",
"StatusValue", "NumberValue", "Text",
"Direction","RefreshData",
"ChangedText","Content","SendText"
};
public static List<ControlName> GetCustomerControlProperty(List<FrameworkElement> selectItems)
{
@@ -34,6 +40,46 @@ namespace BeDesignerSCADA.Common

return result;
}

#region 查找属性
/// <summary>
/// 获取绑定
/// </summary>
/// <param name="selectproperty"></param>
/// <param name="control"></param>
/// <returns></returns>
public static string GetBindString(DependencyProperty selectproperty,FrameworkElement control)
{
try
{
Binding binding = BindingOperations.GetBinding(control, selectproperty);
return binding==null?"":BindingOperations.GetBinding(control, selectproperty).Path.Path;
}
catch (Exception ex)
{
return "";
}
}

/// <summary>
/// 获取一个对象中所有的依赖项属性。
/// </summary>
public static IEnumerable<DependencyProperty> GetDependencyProperties(object instance)
=> TypeDescriptor.GetProperties(instance, new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.All) })
.OfType<PropertyDescriptor>()
.Select(x => DependencyPropertyDescriptor.FromProperty(x)?.DependencyProperty)
.Where(x => x != null);

/// <summary>
/// 获取一个类型中所有的依赖项属性。
/// </summary>
public static IEnumerable<DependencyProperty> GetDependencyProperties(Type type)
=> TypeDescriptor.GetProperties(type, new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.All) })
.OfType<PropertyDescriptor>()
.Select(x => DependencyPropertyDescriptor.FromProperty(x)?.DependencyProperty)
.Where(x => x != null);
#endregion
}

public class ControlName
@@ -50,4 +96,39 @@ namespace BeDesignerSCADA.Common

public IEnumerable<ControlName> Properties { get; set; }
}


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);
}
}
}

+ 1
- 0
BeDesignerSCADA/Controls/MainCanvasPanel.xaml ファイルの表示

@@ -405,6 +405,7 @@
<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="ClearBingToggleButton_Click"></ToggleButton>
<ToggleButton Grid.Column="1" Height="20" Content="绑定" Width="30" Margin="5,0,5,0" HorizontalAlignment="Left" Click="BingToggleButton_Click"></ToggleButton>
</Grid>
</Grid>


+ 29
- 34
BeDesignerSCADA/Controls/MainCanvasPanel.xaml.cs ファイルの表示

@@ -1,4 +1,5 @@
using BeDesignerSCADA.ViewModel;
using BeDesignerSCADA.Common;
using BeDesignerSCADA.ViewModel;
using BeDesignerSCADA.ViewModel;
using BPASmart.Model;
using BPASmartClient.Compiler;
@@ -449,6 +450,31 @@ namespace BeDesignerSCADA.Controls
}
}
/// <summary>
/// 清除绑定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ClearBingToggleButton_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.ClearBingEdit(propertyGridCommand);
}
}
}
catch (Exception ex)
{

}
}
/// <summary>
/// 变量选择
/// </summary>
/// <param name="sender"></param>
@@ -544,42 +570,11 @@ namespace BeDesignerSCADA.Controls
communication?.CommunicationDevices?.ToList().ForEach(x => { viewModel.DevNameList.Add(x.DeviceName); });
}


#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);
}
}

}

+ 2
- 1
BeDesignerSCADA/View/ChildEditWindow.xaml.cs ファイルの表示

@@ -1,4 +1,5 @@
using BeDesignerSCADA.Controls;
using BeDesignerSCADA.Common;
using BeDesignerSCADA.Controls;
using System;
using System.Collections.Generic;
using System.Linq;


+ 43
- 0
BeDesignerSCADA/View/PropertyBindingWindow.xaml ファイルの表示

@@ -0,0 +1,43 @@
<Window x:Class="BeDesignerSCADA.View.PropertyBindingWindow"
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="PropertyBindingWindow" Height="160" Width="400"
Background="White"
BorderBrush="#FFDEDEDE"
BorderThickness="1"
ResizeMode="NoResize"
WindowStyle="None"
WindowStartupLocation="CenterOwner">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<TextBlock FontSize="20" Margin="10,10,0,0">属性绑定器</TextBlock>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Grid>
<TextBlock x:Name="ProperpName" FontSize="16" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center">
属性名称
</TextBlock>
</Grid>
<Grid Grid.Column="1">
<ComboBox x:Name="BindingName" Grid.Column="1" IsEditable="True" ></ComboBox>
</Grid>
</Grid>

<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Width="80" Height="30" Content="清除绑定" x:Name="ClearBtn" Click="ClearBtn_Click" Margin="0,0,10,0"/>
<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>

+ 71
- 0
BeDesignerSCADA/View/PropertyBindingWindow.xaml.cs ファイルの表示

@@ -0,0 +1,71 @@
using BeDesignerSCADA.Common;
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.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BeDesignerSCADA.View
{
/// <summary>
/// PropertyBindingWindow.xaml 的交互逻辑
/// </summary>
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();


public static string ShowEdit(DependencyProperty selectproperty, FrameworkElement control)
{
// 获取所有控件的可用属性
var pros = PropertyHelper.GetBindString(selectproperty, control);
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();
}
}
}

+ 56
- 7
BeDesignerSCADA/ViewModel/MainViewModelNew.cs ファイルの表示

@@ -22,6 +22,8 @@ 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;

@@ -213,6 +215,7 @@ namespace BeDesignerSCADA.ViewModel
/// 当前运行界面
/// </summary>
public RunCanvas runCanvas;

/// <summary>
/// 加载
/// </summary>
@@ -223,6 +226,7 @@ namespace BeDesignerSCADA.ViewModel

runCanvas = objRun as RunCanvas;
}

/// <summary>
/// 编辑
/// </summary>
@@ -232,6 +236,7 @@ namespace BeDesignerSCADA.ViewModel
string result = JsEditWindow.ShowEdit(((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value?.ToString(), canvasPanel.GetAllExecChildren());
((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = result;
}

/// <summary>
/// 子控件编辑
/// </summary>
@@ -241,22 +246,66 @@ namespace BeDesignerSCADA.ViewModel
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");
//父控件
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();

//string xml = ChildEditWindow.ShowEdit(((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value?.ToString());
//((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = xml;
//选中属性
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);
}
}
}

/// <summary>
/// 清除绑定
/// </summary>
/// <param name="obj"></param>
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);
}
}

/// <summary>
/// 图片选择路径
/// </summary>
@@ -270,8 +319,8 @@ namespace BeDesignerSCADA.ViewModel
((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = ofd.FileName;
}
}
#endregion


#endregion
}
}

読み込み中…
キャンセル
保存