高亮节点、取消前面的[n]序号、参考位置,保存位置,参数区为只读

This commit is contained in:
zhengxuan.zhang
2026-04-24 10:44:22 +08:00
parent 9911566675
commit f3e77562b1
5 changed files with 245 additions and 13 deletions
+11 -1
View File
@@ -366,11 +366,21 @@ namespace XplorePlane.Services.Cnc
var result = new List<CncNode>(nodes.Count);
for (int i = 0; i < nodes.Count; i++)
{
result.Add(nodes[i] with { Index = i });
result.Add(ApplyDefaultNodeName(nodes[i] with { Index = i }, i));
}
return result.AsReadOnly();
}
private static CncNode ApplyDefaultNodeName(CncNode node, int index)
{
return node switch
{
ReferencePointNode referencePointNode => referencePointNode with { Name = $"参考点_{index}" },
SavePositionNode savePositionNode => savePositionNode with { Name = $"保存位置_{index}" },
_ => node
};
}
/// <summary>创建参考点节点 | Create reference point node</summary>
private ReferencePointNode CreateReferencePointNode(Guid id, int index)
{
@@ -416,6 +416,8 @@ namespace XplorePlane.ViewModels.Cnc
private void RefreshNodes()
{
NormalizeDefaultNodeNamesInCurrentProgram();
var selectedId = _preferredSelectedNodeId ?? SelectedNode?.Id;
var expansionState = Nodes.ToDictionary(node => node.Id, node => node.IsExpanded);
@@ -466,6 +468,34 @@ namespace XplorePlane.ViewModels.Cnc
_preferredSelectedNodeId = null;
}
private void NormalizeDefaultNodeNamesInCurrentProgram()
{
if (_currentProgram?.Nodes == null || _currentProgram.Nodes.Count == 0)
{
return;
}
var normalizedNodes = _currentProgram.Nodes
.Select((node, index) => ApplyDefaultNodeName(node with { Index = index }, index))
.ToList()
.AsReadOnly();
bool changed = false;
for (int i = 0; i < normalizedNodes.Count; i++)
{
if (!Equals(normalizedNodes[i], _currentProgram.Nodes[i]))
{
changed = true;
break;
}
}
if (changed)
{
_currentProgram = _currentProgram with { Nodes = normalizedNodes };
}
}
private int ResolveInsertAfterIndex(CncNodeType nodeType)
{
if (_currentProgram == null || _currentProgram.Nodes.Count == 0)
@@ -714,7 +744,7 @@ namespace XplorePlane.ViewModels.Cnc
private CncProgram ReplaceProgramNodes(List<CncNode> nodes)
{
var renumberedNodes = nodes
.Select((node, index) => node with { Index = index })
.Select((node, index) => ApplyDefaultNodeName(node with { Index = index }, index))
.ToList()
.AsReadOnly();
@@ -725,6 +755,16 @@ namespace XplorePlane.ViewModels.Cnc
};
}
private static CncNode ApplyDefaultNodeName(CncNode node, int index)
{
return node switch
{
ReferencePointNode referencePointNode => referencePointNode with { Name = $"参考点_{index}" },
SavePositionNode savePositionNode => savePositionNode with { Name = $"保存位置_{index}" },
_ => node
};
}
private static bool IsSavePositionChild(CncNodeType type)
{
return type is CncNodeType.InspectionMarker
@@ -37,6 +37,8 @@ namespace XplorePlane.ViewModels.Cnc
set => UpdateModel(_model with { Name = value ?? string.Empty });
}
public bool IsReadOnlyNodeProperties => IsReferencePoint || IsSavePosition;
public CncNodeType NodeType => _model.NodeType;
public string NodeTypeDisplay => NodeType.ToString();
+17 -8
View File
@@ -15,6 +15,8 @@
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter" />
<local:InverseBooleanToVisibilityConverter x:Key="InverseBoolToVisibilityConverter" />
<local:BoolToDisplayTextConverter x:Key="BoolToDisplayTextConverter" />
<local:NullToVisibilityConverter x:Key="NullToVisibilityConverter" />
<SolidColorBrush x:Key="PanelBg" Color="White" />
@@ -58,6 +60,19 @@
<Setter Property="FontSize" Value="11" />
</Style>
<Style x:Key="DisplayValueLabel" TargetType="Label">
<Setter Property="Height" Value="28" />
<Setter Property="Padding" Value="8,3" />
<Setter Property="Margin" Value="0,0,0,8" />
<Setter Property="Background" Value="#F7F8FA" />
<Setter Property="BorderBrush" Value="#D9DDE3" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="FontFamily" Value="{StaticResource UiFont}" />
<Setter Property="FontSize" Value="11" />
<Setter Property="Foreground" Value="#333333" />
</Style>
<Style x:Key="EditorCheck" TargetType="CheckBox">
<Setter Property="Margin" Value="0,2,0,8" />
<Setter Property="FontFamily" Value="{StaticResource UiFont}" />
@@ -238,12 +253,6 @@
Margin="3,0,0,0"
VerticalAlignment="Center"
Orientation="Horizontal">
<TextBlock
VerticalAlignment="Center"
FontFamily="{StaticResource UiFont}"
FontSize="10"
Foreground="#888888"
Text="{Binding Index, StringFormat='[{0}] '}" />
<TextBlock
VerticalAlignment="Center"
FontFamily="{StaticResource UiFont}"
@@ -310,11 +319,11 @@
<UniformGrid Margin="0,0,0,8" Columns="2">
<StackPanel Margin="0,0,6,0">
<TextBlock Style="{StaticResource LabelStyle}" Text="索引" />
<TextBox Style="{StaticResource EditorBox}" IsReadOnly="True" Text="{Binding SelectedNode.Index, Mode=OneWay}" />
<Label Style="{StaticResource DisplayValueLabel}" Content="{Binding SelectedNode.Index, Mode=OneWay}" />
</StackPanel>
<StackPanel>
<TextBlock Style="{StaticResource LabelStyle}" Text="类型" />
<TextBox Style="{StaticResource EditorBox}" IsReadOnly="True" Text="{Binding SelectedNode.NodeTypeDisplay, Mode=OneWay}" />
<Label Style="{StaticResource DisplayValueLabel}" Content="{Binding SelectedNode.NodeTypeDisplay, Mode=OneWay}" />
</StackPanel>
</UniformGrid>
+174 -3
View File
@@ -1,11 +1,13 @@
using Prism.Ioc;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using XP.Common.Logging.Interfaces;
using XplorePlane.Services;
using XplorePlane.ViewModels.Cnc;
@@ -21,8 +23,13 @@ namespace XplorePlane.Views.Cnc
private static readonly Brush SelectedNodeBorder = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#9CB9D1"));
private static readonly Brush HoverNodeBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#F6FAFC"));
private static readonly Brush HoverNodeBorder = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#D7E4EE"));
private static readonly Brush SelectedNodeForeground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#1F4E79"));
private static readonly Brush DefaultNodeForeground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#202020"));
private static readonly Brush TransparentBrush = Brushes.Transparent;
private CncInspectionModulePipelineViewModel _inspectionModulePipelineViewModel;
private readonly Dictionary<TextBox, Label> _textDisplayLabels = new();
private readonly Dictionary<CheckBox, Label> _checkDisplayLabels = new();
public CncPageView()
{
@@ -74,6 +81,7 @@ namespace XplorePlane.Views.Cnc
CncTreeView.LayoutUpdated -= CncTreeView_LayoutUpdated;
CncTreeView.LayoutUpdated += CncTreeView_LayoutUpdated;
UpdateNodeVisualState();
UpdatePropertyEditorState();
}
private void CncTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
@@ -84,6 +92,7 @@ namespace XplorePlane.Views.Cnc
}
UpdateNodeVisualState();
UpdatePropertyEditorState();
}
private void CncTreeView_PreviewKeyDown(object sender, KeyEventArgs e)
@@ -116,6 +125,7 @@ namespace XplorePlane.Views.Cnc
viewModel.SelectedNode = nodeVm;
UpdateNodeVisualState();
UpdatePropertyEditorState();
CncTreeView.ContextMenu = new ContextMenu
{
@@ -123,13 +133,13 @@ namespace XplorePlane.Views.Cnc
{
new MenuItem
{
Header = "在上方插入位置",
Header = "\u5728\u4E0A\u65B9\u63D2\u5165\u4F4D\u7F6E",
Command = viewModel.PrepareInsertAboveCommand,
CommandParameter = nodeVm
},
new MenuItem
{
Header = "在下方插入位置",
Header = "\u5728\u4E0B\u65B9\u63D2\u5165\u4F4D\u7F6E",
Command = viewModel.PrepareInsertBelowCommand,
CommandParameter = nodeVm
}
@@ -141,6 +151,7 @@ namespace XplorePlane.Views.Cnc
{
HideInlineDeleteButtons();
UpdateNodeVisualState();
UpdatePropertyEditorState();
}
private void HideInlineDeleteButtons()
@@ -174,20 +185,154 @@ namespace XplorePlane.Views.Cnc
{
card.Background = SelectedNodeBackground;
card.BorderBrush = SelectedNodeBorder;
ApplyNodeTextForeground(card, SelectedNodeForeground);
}
else if (card.IsMouseOver)
{
card.Background = HoverNodeBackground;
card.BorderBrush = HoverNodeBorder;
ApplyNodeTextForeground(card, DefaultNodeForeground);
}
else
{
card.Background = TransparentBrush;
card.BorderBrush = TransparentBrush;
ApplyNodeTextForeground(card, DefaultNodeForeground);
}
}
}
private void UpdatePropertyEditorState()
{
if (DataContext is not CncEditorViewModel viewModel)
{
return;
}
var propertyEditorRoot = FindPropertyEditorRoot();
if (propertyEditorRoot == null)
{
return;
}
bool isReadOnlyNode = viewModel.SelectedNode?.IsReadOnlyNodeProperties == true;
foreach (var textBox in FindVisualDescendants<TextBox>(propertyEditorRoot))
{
var bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);
string bindingPath = bindingExpression?.ParentBinding?.Path?.Path ?? string.Empty;
if (string.IsNullOrWhiteSpace(bindingPath))
{
continue;
}
bool alwaysDisplay = bindingPath is "SelectedNode.Index" or "SelectedNode.NodeTypeDisplay";
var label = EnsureTextDisplayLabel(textBox, bindingPath);
bool showLabel = alwaysDisplay || isReadOnlyNode;
textBox.IsReadOnly = showLabel;
textBox.Visibility = showLabel ? Visibility.Collapsed : Visibility.Visible;
label.Visibility = showLabel ? Visibility.Visible : Visibility.Collapsed;
}
foreach (var checkBox in FindVisualDescendants<CheckBox>(propertyEditorRoot))
{
var bindingExpression = checkBox.GetBindingExpression(ToggleButton.IsCheckedProperty);
string bindingPath = bindingExpression?.ParentBinding?.Path?.Path ?? string.Empty;
if (string.IsNullOrWhiteSpace(bindingPath))
{
continue;
}
var label = EnsureCheckDisplayLabel(checkBox, bindingPath);
checkBox.IsEnabled = !isReadOnlyNode;
checkBox.Visibility = isReadOnlyNode ? Visibility.Collapsed : Visibility.Visible;
label.Visibility = isReadOnlyNode ? Visibility.Visible : Visibility.Collapsed;
}
}
private Label EnsureTextDisplayLabel(TextBox textBox, string bindingPath)
{
if (_textDisplayLabels.TryGetValue(textBox, out var existingLabel))
{
return existingLabel;
}
var label = new Label
{
Style = TryFindResource("DisplayValueLabel") as Style,
Visibility = Visibility.Collapsed
};
label.SetBinding(ContentProperty, new Binding(bindingPath));
InsertCompanionControl(textBox, label);
_textDisplayLabels[textBox] = label;
return label;
}
private Label EnsureCheckDisplayLabel(CheckBox checkBox, string bindingPath)
{
if (_checkDisplayLabels.TryGetValue(checkBox, out var existingLabel))
{
return existingLabel;
}
var label = new Label
{
Style = TryFindResource("DisplayValueLabel") as Style,
Visibility = Visibility.Collapsed
};
label.SetBinding(ContentProperty, new Binding(bindingPath)
{
Converter = TryFindResource("BoolToDisplayTextConverter") as IValueConverter
});
InsertCompanionControl(checkBox, label);
_checkDisplayLabels[checkBox] = label;
return label;
}
private static void InsertCompanionControl(Control sourceControl, Control companionControl)
{
if (VisualTreeHelper.GetParent(sourceControl) is not Panel panel)
{
return;
}
if (panel.Children.Contains(companionControl))
{
return;
}
int index = panel.Children.IndexOf(sourceControl);
panel.Children.Insert(index + 1, companionControl);
}
private static void ApplyNodeTextForeground(Border card, Brush foreground)
{
foreach (var textBlock in FindVisualDescendants<TextBlock>(card))
{
if (textBlock.Visibility == Visibility.Visible)
{
textBlock.Foreground = foreground;
}
}
}
private DependencyObject FindPropertyEditorRoot()
{
foreach (var scrollViewer in FindVisualDescendants<ScrollViewer>(this))
{
if (Grid.GetColumn(scrollViewer) == 2)
{
return scrollViewer;
}
}
return null;
}
private static Border FindNodeCard(DependencyObject root)
{
foreach (var border in FindVisualDescendants<Border>(root))
@@ -260,4 +405,30 @@ namespace XplorePlane.Views.Cnc
throw new NotSupportedException();
}
}
public class InverseBooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is true ? Visibility.Collapsed : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
public class BoolToDisplayTextConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is true ? "\u662F" : "\u5426";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
}