解决TreeView的TreeView_SelectedItemChanged事件被多次调用的问题
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
using DMS.Helper;
|
||||
using DMS.Services;
|
||||
using DMS.WPF.Helper;
|
||||
using DMS.WPF.ViewModels.Dialogs;
|
||||
using DMS.WPF.ViewModels.Items;
|
||||
using iNKORE.UI.WPF.Modern.Controls;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using DMS.WPF.ViewModels.Dialogs;
|
||||
using iNKORE.UI.WPF.Modern.Controls;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace DMS.WPF.Views.Dialogs;
|
||||
|
||||
@@ -10,41 +16,67 @@ namespace DMS.WPF.Views.Dialogs;
|
||||
/// </summary>
|
||||
public partial class ImportOpcUaDialog : ContentDialog
|
||||
{
|
||||
private const int ContentAreaMaxWidth = 1200;
|
||||
private const int ContentAreaMaxHeight = 800;
|
||||
|
||||
|
||||
|
||||
|
||||
public ImportOpcUaDialog()
|
||||
{
|
||||
InitializeComponent();
|
||||
this.Opened += OnOpened;
|
||||
}
|
||||
|
||||
private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
|
||||
private void OnOpened(ContentDialog sender, ContentDialogOpenedEventArgs args)
|
||||
{
|
||||
// 在这里处理导入逻辑,例如获取选中的变量
|
||||
// ViewModel.ImportSelectedVariables();
|
||||
}
|
||||
|
||||
private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
|
||||
{
|
||||
// 处理取消逻辑
|
||||
//修改对话框内容的最大宽度和最大高度
|
||||
var backgroundElementBorder = VisualTreeFinder.FindVisualChildByName<Border>(this, "BackgroundElement");
|
||||
backgroundElementBorder.MaxWidth = ContentAreaMaxWidth;
|
||||
backgroundElementBorder.MaxWidth = ContentAreaMaxHeight;
|
||||
}
|
||||
|
||||
private async void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
|
||||
{
|
||||
// if (e.NewValue is OpcUaNode selectedNode)
|
||||
// {
|
||||
// await ViewModel.LoadNodeVariables(selectedNode);
|
||||
// }
|
||||
try
|
||||
{
|
||||
//防止多次调用
|
||||
object selectedObj = this.treeView.SelectedItem;
|
||||
if (SelectedItemChanged != null)
|
||||
{
|
||||
Dispatcher.BeginInvoke(DispatcherPriority.Background, SelectedItemChanged, selectedObj);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
NotificationHelper.ShowError($"选择节点时发生了错误:{ex.Message}");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//事件
|
||||
public async void SelectedItemChanged(object selectedObj)
|
||||
{
|
||||
if (selectedObj is OpcUaNodeItemViewModel selectedNode)
|
||||
{
|
||||
|
||||
if (this.DataContext is ImportOpcUaDialogViewModel viewModel)
|
||||
{
|
||||
await viewModel.LoadNodeVariables(selectedNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs args)
|
||||
{
|
||||
// if (args.AddedItems!=null && args.AddedItems.Count>0)
|
||||
// {
|
||||
// foreach (var item in args.AddedItems)
|
||||
// {
|
||||
// ViewModel.SelectedVariables.Add((Variable)item);
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// if (args.AddedItems!=null && args.AddedItems.Count>0)
|
||||
// {
|
||||
// foreach (var item in args.AddedItems)
|
||||
// {
|
||||
// ViewModel.SelectedVariables.Add((Variable)item);
|
||||
// }
|
||||
//
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user