增加保温层和整理管线的功能,修复自动保存功能等修复多个bug

This commit is contained in:
GG Z
2024-10-27 00:19:48 +08:00
parent b6647218be
commit 77655c9ef5
67 changed files with 3159 additions and 731 deletions

View File

@@ -0,0 +1,247 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
namespace Wpf.Ui.Extend.Controls;
public class ExtendedTreeView : TreeView
{
public event MouseEventHandler? OnHierarchyMouseUp;
public event RoutedEventHandler? OnChecked;
public static readonly DependencyProperty IsExpandedPathProperty = DependencyProperty.Register("IsExpandedPath", typeof(string), typeof(ExtendedTreeView), new PropertyMetadata());
public static readonly DependencyProperty IsSelectedPathProperty = DependencyProperty.Register("IsSelectedPath", typeof(string), typeof(ExtendedTreeView), new PropertyMetadata());
public static readonly DependencyProperty IsCheckedPathProperty = DependencyProperty.Register("IsCheckedPath", typeof(string), typeof(ExtendedTreeView), new PropertyMetadata());
public string IsSelectedPath
{
get { return (string)GetValue(IsSelectedPathProperty); }
set { SetValue(IsSelectedPathProperty, value); }
}
public string IsExpandedPath
{
get { return (string)GetValue(IsExpandedPathProperty); }
set { SetValue(IsExpandedPathProperty, value); }
}
public string IsCheckedPath
{
get { return (string)GetValue(IsCheckedPathProperty); }
set { SetValue(IsCheckedPathProperty, value); }
}
protected override DependencyObject GetContainerForItemOverride()
{
ExtendedTreeViewItem treeViewItem = null;
if (IsCheckedPath == null)
{
treeViewItem = ExtendedTreeViewItem.CreateItemWithBinding(IsExpandedPath, IsSelectedPath);
}
else
{
var xtreeViewItem = CheckableTreeViewItem.CreateItemWithBinding(IsExpandedPath, IsSelectedPath, IsCheckedPath);
xtreeViewItem.IsCheckedHandler += XtreeViewItem_IsCheckedHandler;
treeViewItem = xtreeViewItem;
}
treeViewItem.OnHierarchyMouseUp += OnChildItemMouseLeftButtonUp;
return treeViewItem;
}
private void XtreeViewItem_IsCheckedHandler(object sender, RoutedEventArgs e)
{
if (this.OnChecked != null)
{
this.OnChecked(sender, e);
e.Handled = true;
}
}
private void OnChildItemMouseLeftButtonUp(object sender, MouseEventArgs e)
{
if (this.OnHierarchyMouseUp != null)
{
this.OnHierarchyMouseUp(this, e);
e.Handled = true;
}
}
}
public class ExtendedTreeViewItem : TreeViewItem
{
private string isExpandedPath;
private string isSelectedPath;
public ExtendedTreeViewItem()
{
this.MouseLeftButtonUp += OnMouseLeftButtonUp;
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
}
public ExtendedTreeViewItem(string isExpandedPath, string isSelectedPath)
{
this.isExpandedPath = isExpandedPath;
this.isSelectedPath = isSelectedPath;
}
void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
HierarchyMouseUp(e);
}
protected override DependencyObject GetContainerForItemOverride()
{
var childItem = CreateItemWithBinding(isExpandedPath, isSelectedPath);
childItem.MouseLeftButtonUp += OnMouseLeftButtonUp;
return childItem;
}
public static ExtendedTreeViewItem CreateItemWithBinding(string isExpandedPath, string isSelectedPath)
{
var tvi = new ExtendedTreeViewItem(isExpandedPath, isSelectedPath);
var expandedBinding = new Binding(isExpandedPath)
{
Mode = BindingMode.TwoWay
};
tvi.SetBinding(TreeViewItem.IsExpandedProperty, expandedBinding);
var selectedBinding = new Binding(isSelectedPath)
{
Mode = BindingMode.TwoWay
};
tvi.SetBinding(TreeViewItem.IsSelectedProperty, selectedBinding);
return tvi;
}
public event MouseEventHandler OnHierarchyMouseUp;
protected void HierarchyMouseUp(MouseButtonEventArgs e)
{
if (this.OnHierarchyMouseUp != null)
{
this.OnHierarchyMouseUp?.Invoke(this, e);
e.Handled = true;
}
}
}
public class CheckableTreeViewItem : ExtendedTreeViewItem
{
private string isExpandedPath;
private string isSelectedPath;
private string isCheckedPath;
private CheckBox? checkBox;
public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(CheckableTreeViewItem), new PropertyMetadata(false));
static CheckableTreeViewItem()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckableTreeViewItem), new FrameworkPropertyMetadata(typeof(CheckableTreeViewItem)));
}
public CheckableTreeViewItem()
{
}
public override void OnApplyTemplate()
{
checkBox = this.GetTemplateChild("PART_CheckBox") as CheckBox;
checkBox.Checked += CheckBox_Checked;
checkBox.Unchecked += CheckBox_Unchecked;
base.OnApplyTemplate();
}
private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
if (IsCheckedHandler != null)
{
IsCheckedHandler?.Invoke(this, e);
e.Handled = true;
}
}
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
if (IsCheckedHandler != null)
{
IsCheckedHandler?.Invoke(this, e);
e.Handled = true;
}
}
public CheckableTreeViewItem(string isExpandedPath, string isSelectedPath, string isCheckedPath)
{
this.isExpandedPath = isExpandedPath;
this.isSelectedPath = isSelectedPath;
this.isCheckedPath = isCheckedPath;
}
public bool IsChecked
{
get { return (bool)GetValue(IsCheckedProperty); }
set { SetValue(IsCheckedProperty, value); }
}
protected override DependencyObject GetContainerForItemOverride()
{
var childItem = CreateItemWithBinding(isExpandedPath, isSelectedPath, isCheckedPath);
childItem.IsCheckedHandler += ChildItem_IsCheckedHandler;
return childItem;
}
private void ChildItem_IsCheckedHandler(object sender, RoutedEventArgs e)
{
if (IsCheckedHandler != null)
{
IsCheckedHandler?.Invoke(sender, e);
e.Handled = true;
}
}
public static CheckableTreeViewItem CreateItemWithBinding(string isExpandedPath, string isSelectedPath, string isCheckedPath)
{
var tvi = new CheckableTreeViewItem(isExpandedPath, isSelectedPath, isCheckedPath);
var expandedBinding = new Binding(isExpandedPath)
{
Mode = BindingMode.TwoWay
};
tvi.SetBinding(TreeViewItem.IsExpandedProperty, expandedBinding);
var selectedBinding = new Binding(isSelectedPath)
{
Mode = BindingMode.TwoWay
};
tvi.SetBinding(TreeViewItem.IsSelectedProperty, selectedBinding);
var isCheckedBinding = new Binding(isCheckedPath)
{
Mode = BindingMode.TwoWay
};
tvi.SetBinding(CheckableTreeViewItem.IsCheckedProperty, isCheckedBinding);
return tvi;
}
public event RoutedEventHandler IsCheckedHandler;
}

View File

@@ -0,0 +1,155 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace Wpf.Ui.Extend.Controls;
public class TreeComboBox : ComboBox
{
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(IEnumerable), typeof(TreeComboBox), new PropertyMetadata(null));
public static readonly DependencyProperty ParentPathProperty = DependencyProperty.Register("ParentPath", typeof(string), typeof(TreeComboBox), new PropertyMetadata());
public static readonly DependencyProperty SelectedNodeProperty = DependencyProperty.Register("SelectedNode", typeof(object), typeof(TreeComboBox), new FrameworkPropertyMetadata(default));
public static readonly DependencyProperty IsCheckedPathProperty = DependencyProperty.Register("IsCheckedPath", typeof(string), typeof(TreeComboBox), new PropertyMetadata());
public static readonly DependencyProperty IsExpandedPathProperty = DependencyProperty.Register("IsExpandedPath", typeof(string), typeof(TreeComboBox), new PropertyMetadata("IsExpanded"));
public static readonly DependencyProperty IsSelectedPathProperty = DependencyProperty.Register("IsSelectedPath", typeof(string), typeof(TreeComboBox), new PropertyMetadata("IsSelected"));
private ExtendedTreeView _treeView;
private ObservableCollection<object> list = [];
static TreeComboBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TreeComboBox), new FrameworkPropertyMetadata(typeof(TreeComboBox)));
}
protected override void OnMouseWheel(MouseWheelEventArgs e)
{
//don't call the method of the base class
}
public override void OnApplyTemplate()
{
_treeView = (ExtendedTreeView)this.GetTemplateChild("treeView");
_treeView.OnHierarchyMouseUp += new MouseEventHandler(OnTreeViewHierarchyMouseUp);
_treeView.OnChecked += _treeView_OnChecked;
this.UpdateSelectedItem();
base.OnApplyTemplate();
}
private void _treeView_OnChecked(object sender, RoutedEventArgs e)
{
if (sender is CheckableTreeViewItem { IsChecked: bool isChecked } item)
{
if (isChecked)
{
list.Add(item.DataContext);
}
else if (list.Contains(item))
{
list.Remove(item.DataContext);
}
SelectedItems = list;
}
}
protected override void OnDropDownClosed(EventArgs e)
{
base.OnDropDownClosed(e);
this.UpdateSelectedItem();
}
protected override void OnDropDownOpened(EventArgs e)
{
base.OnDropDownOpened(e);
this.UpdateSelectedItem();
}
/// <summary>
/// Handles clicks on any item in the tree view
/// </summary>
private void OnTreeViewHierarchyMouseUp(object sender, MouseEventArgs e)
{
var hierarchy = SelectItems();
this.SelectedItem = hierarchy.First();
this.SelectedItems = hierarchy;
UpdateSelectedItem();
this.IsDropDownOpen = false;
this.SelectedNode = _treeView.SelectedItem;
}
#region properties
public string IsExpandedPath
{
get { return (string)GetValue(IsExpandedPathProperty); }
set { SetValue(IsExpandedPathProperty, value); }
}
public string IsSelectedPath
{
get { return (string)GetValue(IsSelectedPathProperty); }
set { SetValue(IsSelectedPathProperty, value); }
}
public string IsCheckedPath
{
get { return (string)GetValue(IsCheckedPathProperty); }
set { SetValue(IsCheckedPathProperty, value); }
}
public string ParentPath
{
get { return (string)GetValue(ParentPathProperty); }
set { SetValue(ParentPathProperty, value); }
}
public object SelectedNode
{
get { return GetValue(SelectedNodeProperty); }
set { SetValue(SelectedNodeProperty, value); }
}
public IEnumerable SelectedItems
{
get { return (IEnumerable)GetValue(SelectedItemsProperty); }
set { SetValue(SelectedItemsProperty, value); }
}
#endregion properties
private void UpdateSelectedItem()
{
if (_treeView.SelectedItem != null)
{
var hierarchy = SelectItems();
SelectedItems = hierarchy;
SelectedNode = hierarchy.Last();
}
}
private object[] SelectItems()
{
var type = _treeView.SelectedItem.GetType();
var propInfo = type.GetProperty(ParentPath);
return TreeHelper.GetAncestors(_treeView.SelectedItem, a => propInfo.GetValue(a)).Reverse().ToArray();
}
static class TreeHelper
{
public static IEnumerable<object> GetAncestors(object vm, Func<object, object> parent)
{
yield return vm;
while (parent(vm) != null)
{
yield return parent(vm);
vm = parent(vm);
}
}
}
}

View File

@@ -0,0 +1,130 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:exc="clr-namespace:Wpf.Ui.Extend.Controls">
<Style TargetType="{x:Type exc:TreeComboBox}">
<!--<Setter Property="SnapsToDevicePixels"
Value="true" />-->
<!--<Setter Property="OverridesDefaultStyle"
Value="true" />-->
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.CanContentScroll" Value="true" />
<Setter Property="MinWidth" Value="120" />
<Setter Property="MinHeight" Value="20" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type exc:TreeComboBox}">
<Grid>
<ToggleButton
x:Name="ToggleButton"
ClickMode="Press"
Focusable="false"
IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
Template="{StaticResource ComboBoxToggleButton}" />
<Grid IsHitTestVisible="False">
<ItemsControl
x:Name="ContentSite"
Margin="3,3,23,3"
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
ItemTemplate="{TemplateBinding ItemTemplate}"
ItemTemplateSelector="{TemplateBinding ItemTemplateSelector}"
ItemsSource="{Binding SelectedItems, RelativeSource={RelativeSource Mode=TemplatedParent}}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
<TextBox
x:Name="PART_EditableTextBox"
Margin="3,3,23,3"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
Background="Transparent"
Focusable="True"
IsReadOnly="{TemplateBinding IsReadOnly}"
Style="{x:Null}"
Template="{StaticResource ComboBoxTextBox}"
Visibility="Hidden" />
<Popup
x:Name="Popup"
AllowsTransparency="True"
Focusable="False"
IsOpen="{TemplateBinding IsDropDownOpen}"
Placement="Bottom"
PopupAnimation="Slide">
<Grid
x:Name="DropDown"
MinWidth="{TemplateBinding ActualWidth}"
MaxHeight="{TemplateBinding MaxDropDownHeight}"
SnapsToDevicePixels="True">
<Border x:Name="DropDownBorder" BorderThickness="1">
<!--<Border.BorderBrush>
<SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
</Border.BorderBrush>
<Border.Background>
<SolidColorBrush Color="{DynamicResource ControlLightColor}" />
</Border.Background>-->
</Border>
<ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
<exc:ExtendedTreeView
x:Name="treeView"
IsCheckedPath="{TemplateBinding IsCheckedPath}"
IsExpandedPath="{TemplateBinding IsExpandedPath}"
IsSelectedPath="{TemplateBinding IsSelectedPath}"
ItemTemplate="{TemplateBinding ItemTemplate}"
ItemsSource="{TemplateBinding ItemsSource}" />
</ScrollViewer>
</Grid>
</Popup>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver" />
<VisualState x:Name="Disabled">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="PART_EditableTextBox" Storyboard.TargetProperty="(TextElement.Foreground). (SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0" Value="{StaticResource DisabledForegroundColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="EditStates">
<VisualState x:Name="Editable">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="PART_EditableTextBox" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentSite" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Hidden}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Uneditable" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="DropDownBorder" Property="MinHeight" Value="95" />
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false" />
</Trigger>
<Trigger SourceName="Popup" Property="AllowsTransparency" Value="true">
<Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4" />
<Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>