using BPASmartClient.Compiler; 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.Animation; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace BPASmartClient.SCADAControl.CustomerControls { /// /// TheCylinder.xaml 的交互逻辑 /// 气缸 /// public partial class TheCylinder : UserControl, IExecutable { #region 临时变量 Ellipse LeftTog = null; Ellipse RightTog = null; Storyboard storyboard = new Storyboard(); #endregion public TheCylinder() { InitializeComponent(); Width = 389; Height = 100; this.SizeChanged += TheCylinder_SizeChanged; } #region 事件 /// /// 字节改变 /// /// /// /// private void TheCylinder_SizeChanged(object sender, SizeChangedEventArgs e) { if (LeftTog == null) { foreach (Ellipse tb in FindVisualChildren(this)) { if (tb.Tag != null) { if (tb.Tag.ToString() == "LeftTog") LeftTog = tb; else if (tb.Tag.ToString() == "RightTog") RightTog = tb; } } //storyboard.RepeatBehavior = RepeatBehavior.Forever; //DoubleAnimationUsingKeyFrames animation = new DoubleAnimationUsingKeyFrames(); //Storyboard.SetTarget(animation, ellipseControl); //Storyboard.SetTargetProperty(animation, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)")); //animation.KeyFrames.Add(new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0)))); //animation.KeyFrames.Add(new EasingDoubleKeyFrame(180, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1)))); //animation.KeyFrames.Add(new EasingDoubleKeyFrame(360, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2)))); //storyboard.Children.Add(animation); } } public static IEnumerable FindVisualChildren(DependencyObject depObj) where T : DependencyObject { if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { DependencyObject child = VisualTreeHelper.GetChild(depObj, i); if (child != null && child is T) { yield return (T)child; } foreach (T childOfChild in FindVisualChildren(child)) { yield return childOfChild; } } } } #endregion #region 属性 public string ControlType => "控件"; private bool isExecuteState; public bool IsExecuteState { get { return isExecuteState; } set { isExecuteState = value; if (IsExecuteState) { IsEnabled = true; Focusable = true; Register(); } } } public event EventHandler PropertyChange; //声明一个事件 public bool LeftTogIsChecked { get { return (bool)GetValue(LeftTogIsCheckedProperty); } set { SetValue(LeftTogIsCheckedProperty, value); } } public static readonly DependencyProperty LeftTogIsCheckedProperty = DependencyProperty.Register("LeftTogIsChecked", typeof(bool), typeof(TheCylinder), new PropertyMetadata(false, new PropertyChangedCallback(OnPropertyChanged))); public bool RightTogIsChecked { get { return (bool)GetValue(RightTogIsCheckedProperty); } set { SetValue(RightTogIsCheckedProperty, value); } } public static readonly DependencyProperty RightTogIsCheckedProperty = DependencyProperty.Register("RightTogIsChecked", typeof(bool), typeof(TheCylinder), new PropertyMetadata(false, new PropertyChangedCallback(OnPropertyChanged))); private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { (d as TheCylinder)?.Refresh(); } private void Refresh() { if (LeftTog != null) { LeftTog.Fill= LeftTogIsChecked?new SolidColorBrush(Colors.Green): new SolidColorBrush(Colors.Red); RightTog.Fill = RightTogIsChecked ? new SolidColorBrush(Colors.Green) : new SolidColorBrush(Colors.Red); } } /// /// 数据模板 /// private Dictionary DataModel { get { return (Dictionary)GetValue(DataModelProperty); } set { SetValue(DataModelProperty, value); } } private static readonly DependencyProperty DataModelProperty = DependencyProperty.Register("DataModel", typeof(Dictionary), typeof(TheCylinder), new PropertyMetadata(new Dictionary())); #endregion /// /// 运行事件 /// public void Register() { Class_DataBus.GetInstance().BindingAction += BindingActionHeader; } public void BindingActionHeader(object sender, EventArgs e) { this.Dispatcher.Invoke((Action)(() => { DataModel = Class_DataBus.GetInstance().Dic_RedisDataBinding; PropertyChange?.Invoke(this, EventArgs.Empty); })); } } }