Laservall_solidworks_inject/Windows/ColumnConfigWindow.xaml.cs

142 lines
4.1 KiB
C#
Raw Normal View History

using Laservall.Solidworks.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace Laservall.Solidworks.Windows
{
public class InverseBoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool b && b)
{
return Visibility.Collapsed;
}
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public partial class ColumnConfigWindow : Window
{
private ObservableCollection<BomColumnConfig> columns;
private HashSet<string> originalNonFixedKeys;
private HashSet<string> previousRemovedKeys;
public BomSettings ResultSettings { get; private set; }
public ColumnConfigWindow(BomSettings settings)
{
InitializeComponent();
originalNonFixedKeys = new HashSet<string>(
settings.Columns.Where(c => !c.IsFixed).Select(c => c.Name));
previousRemovedKeys = new HashSet<string>(settings.RemovedKeys ?? new HashSet<string>());
columns = new ObservableCollection<BomColumnConfig>(
settings.Columns.Select(c => new BomColumnConfig
{
Name = c.Name,
IsVisible = c.IsVisible,
IsExport = c.IsExport,
IsFixed = c.IsFixed,
IsUserAdded = c.IsUserAdded
})
);
ColumnsGrid.ItemsSource = columns;
}
private void NewPropKeyBox_TextChanged(object sender, TextChangedEventArgs e)
{
NewPropKeyPlaceholder.Visibility =
string.IsNullOrEmpty(NewPropKeyBox.Text)
? Visibility.Visible
: Visibility.Collapsed;
}
private void AddCustomProp_Click(object sender, RoutedEventArgs e)
{
string key = NewPropKeyBox.Text?.Trim();
if (string.IsNullOrEmpty(key))
{
return;
}
if (columns.Any(c => c.Name == key))
{
MessageBox.Show(this, $"列 \"{key}\" 已存在", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
columns.Add(new BomColumnConfig
{
Name = key,
IsVisible = true,
IsExport = true,
IsFixed = false,
IsUserAdded = true
});
previousRemovedKeys.Remove(key);
NewPropKeyBox.Text = "";
}
private void DeleteColumn_Click(object sender, RoutedEventArgs e)
{
if (!(sender is Button btn))
{
return;
}
if (!(btn.DataContext is BomColumnConfig col))
{
return;
}
if (col.IsFixed)
{
return;
}
columns.Remove(col);
}
private void OK_Click(object sender, RoutedEventArgs e)
{
var remainingNames = new HashSet<string>(columns.Select(c => c.Name));
var removedKeys = new HashSet<string>(previousRemovedKeys);
foreach (var key in originalNonFixedKeys)
{
if (!remainingNames.Contains(key))
{
removedKeys.Add(key);
}
}
removedKeys.ExceptWith(remainingNames);
ResultSettings = new BomSettings
{
Columns = columns.ToList(),
RemovedKeys = removedKeys
};
DialogResult = true;
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
}
}
}