134 lines
5.0 KiB
C#
134 lines
5.0 KiB
C#
using Common.Models;
|
||
using MegaRobo.C00225155App.MenuViewModels;
|
||
using MegaRobo.C00225155App.MenuViews.Bottles;
|
||
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 SampleBottleUserControl : UserControl
|
||
{
|
||
//UserControl 的 DataContext 并未设置,而是通过 SampleBottleModel 依赖属性传递数据
|
||
|
||
public SampleBottleModel SampleBottleModel
|
||
{
|
||
get { return (SampleBottleModel)GetValue(SampleBottleModelProperty); }
|
||
set { SetValue(SampleBottleModelProperty, value); }
|
||
}
|
||
|
||
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
|
||
public static readonly DependencyProperty SampleBottleModelProperty =
|
||
DependencyProperty.Register(
|
||
"SampleBottleModel",
|
||
typeof(SampleBottleModel),
|
||
typeof(SampleBottleUserControl),
|
||
new PropertyMetadata(null, OnSampleBottleModelChanged)// 重要:默认值设为 nullKD
|
||
);
|
||
|
||
|
||
public SampleBottleUserControl()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
private static void OnSampleBottleModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||
{
|
||
var control = (SampleBottleUserControl)d;
|
||
control.InvalidateVisual();//强制重绘
|
||
var ctIndicator = control.FindName("MyCTIndicator") as CTIndicator; // 需给 CTIndicator 命名
|
||
if (ctIndicator != null)
|
||
{
|
||
ctIndicator.ForceRefresh();
|
||
// 手动更新CTValue绑定(双重保障)
|
||
BindingOperations.GetBindingExpression(
|
||
ctIndicator, CTIndicator.CTValueProperty
|
||
)?.UpdateTarget();
|
||
}
|
||
}
|
||
|
||
private void Bottle_MouseLeft_old(object sender, MouseButtonEventArgs e)
|
||
{
|
||
//var originalModel = this.ReactBottleModel; // 保存原始模型引用
|
||
var viewModel = new EditSampleBottlePropertiesViewModel { SampleBottle = this.SampleBottleModel };
|
||
var editWindow = new EditSampleBottlePropertiesWindow { DataContext = viewModel };
|
||
|
||
// 显示编辑属性窗口并等待用户操作
|
||
if (editWindow.ShowDialog() == true)
|
||
{
|
||
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
|
||
{
|
||
// 更新自定义控件的属性
|
||
viewModel = (EditSampleBottlePropertiesViewModel)editWindow.DataContext;
|
||
var ctIndicator = this.FindName("MyCTIndicator") as CTIndicator; // 需给 CTIndicator 命名
|
||
if (ctIndicator != null)
|
||
{
|
||
ctIndicator.ForceRefresh();
|
||
}
|
||
}));
|
||
}
|
||
e.Handled = true;
|
||
}
|
||
|
||
private void Bottle_MouseLeft(object sender, MouseButtonEventArgs e)
|
||
{
|
||
// 初始化编辑视图模型 SampleBottleType
|
||
var viewModel = new EditSampleBottlePropertiesViewModel
|
||
{
|
||
SampleBottle = this.SampleBottleModel // 传递当前模型
|
||
};
|
||
var editWindow = new EditSampleBottlePropertiesWindow
|
||
{
|
||
DataContext = viewModel
|
||
};
|
||
|
||
// 显示编辑窗口并处理结果
|
||
if (editWindow.ShowDialog() == true)
|
||
{
|
||
// 低优先级执行刷新,确保编辑事务已释放
|
||
Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
|
||
{
|
||
// 处理父视图的编辑事务(关键)
|
||
var parentView = CollectionViewSource.GetDefaultView(this.DataContext) as ListCollectionView;
|
||
if (parentView != null)
|
||
{
|
||
if (parentView.IsEditingItem) parentView.CommitEdit();
|
||
if (parentView.IsAddingNew) parentView.CommitNew();
|
||
}
|
||
|
||
// 刷新 CTIndicator 控件
|
||
var ctIndicator = this.FindName("MyCTIndicator") as CTIndicator;
|
||
if (ctIndicator != null)
|
||
{
|
||
//ctIndicator.SampleBottle = viewModel.SampleBottle;
|
||
ctIndicator.ForceRefresh();
|
||
}
|
||
}));
|
||
}
|
||
|
||
// 释放窗口资源
|
||
editWindow.DataContext = null;
|
||
editWindow.Close();
|
||
|
||
e.Handled = true;
|
||
}
|
||
|
||
}
|
||
}
|