using Common.Models; 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.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.Navigation; using System.Windows.Shapes; using System.Windows.Threading; namespace MegaRobo.C00225155App.MenuViews { /// /// BottleUserControl.xaml 的交互逻辑 /// public partial class LiquidBottleUserControl : UserControl { public SourceLiquidBottleModel LiquidBottleModel { get { return (SourceLiquidBottleModel)GetValue(LiquidBottleModelProperty); } set { SetValue(LiquidBottleModelProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty LiquidBottleModelProperty = DependencyProperty.Register( "LiquidBottleModel", typeof(SourceLiquidBottleModel), typeof(LiquidBottleUserControl), new PropertyMetadata(null, OnLiquidBottleModelChanged)// 重要:默认值设为 null ); //public Brush BorderBrushColor //{ // get { return (Brush)GetValue(BorderBrushColorProperty); } // set { SetValue(BorderBrushColorProperty, value); } //} //public static readonly DependencyProperty BorderBrushColorProperty = // DependencyProperty.Register( // "BorderBrushColor", // typeof(Brush), // typeof(LiquidBottleUserControl), // new PropertyMetadata(new SolidColorBrush(Color.FromRgb(66, 66, 66)), OnBorderBrushColorChanged) // ); public LiquidBottleUserControl() { InitializeComponent(); } private static void OnLiquidBottleModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var control = (LiquidBottleUserControl)d; if (e.OldValue is SourceLiquidBottleModel oldModel) { oldModel.PropertyChanged -= control.LiquidBottleModel_PropertyChanged; } if (e.NewValue is SourceLiquidBottleModel newModel) { newModel.PropertyChanged += control.LiquidBottleModel_PropertyChanged; } control.UpdateBorderBrushColor(); } private void LiquidBottleModel_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == nameof(SourceLiquidBottleModel.SourceLiquidName)) { UpdateBorderBrushColor(); } } private void UpdateBorderBrushColor() { if (LiquidBottleModel != null) { if (!string.IsNullOrEmpty(LiquidBottleModel.SourceLiquidName)) { LiquidBottleModel.HaveBottle = true; } else { LiquidBottleModel.HaveBottle = false; } } } private void Bottle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // 创建编辑属性窗口实例 var editWindow = new EditLiquidBottlePropertiesWindow { LiquidBottle = this.LiquidBottleModel, }; // 显示编辑属性窗口并等待用户操作 if (editWindow.ShowDialog() == true) { // 使用 Dispatcher 延迟更新,确保当前操作完成 Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => { //更新自定义控件的属性 this.LiquidBottleModel = editWindow.LiquidBottle; // 强制刷新绑定(双重保障) BindingOperations.GetBindingExpression( this, LiquidBottleUserControl.LiquidBottleModelProperty )?.UpdateTarget(); })); } } } }