81 lines
2.9 KiB
C#
81 lines
2.9 KiB
C#
using System.Windows.Controls;
|
|
using System.Windows.Media;
|
|
using System.Windows.Threading;
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
using Seyounth.Hyosung.UI.ViewModels.Pages;
|
|
using Wpf.Ui.Controls;
|
|
using DataGrid = System.Windows.Controls.DataGrid;
|
|
using TextBox = System.Windows.Controls.TextBox;
|
|
|
|
namespace Seyounth.Hyosung.UI.Views.Pages
|
|
{
|
|
public partial class DataPage : INavigableView<DataViewModel>
|
|
{
|
|
public DataViewModel ViewModel { get; }
|
|
|
|
public DataPage(DataViewModel viewModel)
|
|
{
|
|
ViewModel = viewModel;
|
|
DataContext = this;
|
|
|
|
InitializeComponent();
|
|
WeakReferenceMessenger.Default.Register<PalletManagementViewModel.SavePalletCompletedMessage>(this, (r, m) =>
|
|
{
|
|
// 提交当前编辑并取消编辑模式
|
|
VarietyDataGrid.CommitEdit();
|
|
VarietyDataGrid.CancelEdit();
|
|
});
|
|
}
|
|
|
|
private void VarietyDataGrid_AddingNewItem(object? sender, AddingNewItemEventArgs e)
|
|
{
|
|
Dispatcher.BeginInvoke(new Action(() =>
|
|
{
|
|
if (VarietyDataGrid.Items.Count == 0) return;
|
|
|
|
var lastIndex = VarietyDataGrid.Items.Count - 1;
|
|
VarietyDataGrid.ScrollIntoView(VarietyDataGrid.Items[lastIndex]);
|
|
VarietyDataGrid.SelectedIndex = lastIndex;
|
|
|
|
if (VarietyDataGrid.ItemContainerGenerator.ContainerFromIndex(lastIndex) is not DataGridRow row) return;
|
|
row.Focus();
|
|
VarietyDataGrid.CurrentColumn = VarietyDataGrid.Columns[0];
|
|
VarietyDataGrid.BeginEdit();
|
|
}), DispatcherPriority.ContextIdle);
|
|
}
|
|
|
|
private void VarietyDataGrid_CellEditEnding(object? sender, DataGridCellEditEndingEventArgs e)
|
|
{
|
|
if (e.EditAction == DataGridEditAction.Commit)
|
|
{
|
|
var binding = e.EditingElement.GetBindingExpression(TextBox.TextProperty);
|
|
binding?.UpdateSource();
|
|
}
|
|
}
|
|
|
|
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
{
|
|
if (sender is ComboBox comboBox)
|
|
{
|
|
// 获取所在DataGrid行
|
|
var dataGridRow = FindVisualParent<DataGridRow>(comboBox);
|
|
var dataGrid = FindVisualParent<DataGrid>(comboBox);
|
|
if (dataGrid != null && dataGridRow != null && !dataGridRow.IsEditing)
|
|
{
|
|
dataGrid.CurrentItem = dataGridRow.Item;
|
|
dataGrid.BeginEdit();
|
|
}
|
|
}
|
|
}
|
|
|
|
private static T FindVisualParent<T>(DependencyObject child) where T : DependencyObject
|
|
{
|
|
while (child != null && child is not T)
|
|
{
|
|
child = VisualTreeHelper.GetParent(child);
|
|
}
|
|
return child as T;
|
|
}
|
|
}
|
|
}
|