解决TreeView的TreeView_SelectedItemChanged事件被多次调用的问题
This commit is contained in:
@@ -8,10 +8,10 @@
|
||||
xmlns:vm="clr-namespace:DMS.WPF.ViewModels.Dialogs"
|
||||
Title="从OPC UA服务器导入变量"
|
||||
d:DataContext="{d:DesignInstance vm:ImportOpcUaDialogViewModel}"
|
||||
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
|
||||
CloseButtonCommand="{Binding CloseButtonCommand}"
|
||||
CloseButtonText="取消"
|
||||
PrimaryButtonCommand="{Binding PrimaryButtonCommand}"
|
||||
PrimaryButtonText="导入"
|
||||
SecondaryButtonClick="ContentDialog_SecondaryButtonClick"
|
||||
SecondaryButtonText="取消"
|
||||
mc:Ignorable="d">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
@@ -47,6 +47,7 @@
|
||||
|
||||
<!-- 节点树 -->
|
||||
<TreeView
|
||||
Name="treeView"
|
||||
Grid.Row="1"
|
||||
Grid.Column="0"
|
||||
Margin="0,0,10,0"
|
||||
@@ -67,6 +68,18 @@
|
||||
<TextBlock Text="{Binding DisplayName}" />
|
||||
</HierarchicalDataTemplate>
|
||||
</TreeView.ItemTemplate>
|
||||
<TreeView.ItemContainerStyle>
|
||||
<Style BasedOn="{StaticResource {x:Type TreeViewItem}}" TargetType="{x:Type TreeViewItem}">
|
||||
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
|
||||
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
|
||||
<Setter Property="FontWeight" Value="Normal" />
|
||||
<Style.Triggers>
|
||||
<Trigger Property="IsSelected" Value="True">
|
||||
<Setter Property="FontWeight" Value="Bold" />
|
||||
</Trigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</TreeView.ItemContainerStyle>
|
||||
</TreeView>
|
||||
|
||||
<!-- 变量列表 -->
|
||||
|
||||
@@ -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