C00225155-02/C00225155/MegaRobo.C00225155/MegaRobo.C00225155App/MenuViews/Bottles/PowderBottleUserControl.xam...

128 lines
4.3 KiB
C#

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
{
/// <summary>
/// BottleUserControl.xaml 的交互逻辑
/// </summary>
public partial class PowderBottleUserControl : UserControl
{
public SourcePowderBottleModel PowderBottleModel
{
get { return (SourcePowderBottleModel)GetValue(PowderBottleModelProperty); }
set { SetValue(PowderBottleModelProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PowderBottleModelProperty =
DependencyProperty.Register(
"PowderBottleModel",
typeof(SourcePowderBottleModel),
typeof(PowderBottleUserControl),
new PropertyMetadata(null, OnPowderBottleModelChanged)// 重要:默认值设为 null
);
//public Brush BorderBrushColor2
//{
// get { return (Brush)GetValue(BorderBrushColor2Property); }
// set { SetValue(BorderBrushColor2Property, value); }
//}
//public static readonly DependencyProperty BorderBrushColor2Property =
// DependencyProperty.Register(
// "BorderBrushColor2",
// typeof(Brush),
// typeof(PowderBottleUserControl),
// new PropertyMetadata(new SolidColorBrush(Color.FromRgb(66, 66, 66)))
// );
public PowderBottleUserControl()
{
InitializeComponent();
}
private static void OnPowderBottleModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = (PowderBottleUserControl)d;
if (e.OldValue is SourcePowderBottleModel oldModel)
{
oldModel.PropertyChanged -= control.PowderBottleModel_PropertyChanged;
}
if (e.NewValue is SourcePowderBottleModel newModel)
{
newModel.PropertyChanged += control.PowderBottleModel_PropertyChanged;
}
control.UpdateBorderBrushColor();
//control.InvalidateVisual();//强制重绘
}
private void PowderBottleModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(SourcePowderBottleModel.SourcePowderName))
{
UpdateBorderBrushColor();
}
}
private void UpdateBorderBrushColor()
{
if (PowderBottleModel != null)
{
if (!string.IsNullOrEmpty(PowderBottleModel.SourcePowderName))
{
PowderBottleModel.HaveBottle = true;
}
else
{
PowderBottleModel.HaveBottle = false;
}
}
}
private void BorderMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// 创建编辑属性窗口实例
var editWindow = new EditPowderBottlePropertiesWindow
{
PowderBottle = this.PowderBottleModel,
};
// 显示编辑属性窗口并等待用户操作
if (editWindow.ShowDialog() == true)
{
// 使用 Dispatcher 延迟更新,确保当前操作完成
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
// 更新自定义控件的属性
this.PowderBottleModel = editWindow.PowderBottle;
// 强制刷新绑定(双重保障)
BindingOperations.GetBindingExpression(
this,
PowderBottleUserControl.PowderBottleModelProperty
)?.UpdateTarget();
}));
}
}
}
}