83 lines
2.9 KiB
C#
83 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;
|
||
|
|
||
|
namespace Seyounth.Hyosung.UI.Views.Pages;
|
||
|
|
||
|
public partial class PalletManagementPage : Page
|
||
|
{
|
||
|
public PalletManagementViewModel ViewModel { get; }
|
||
|
|
||
|
public PalletManagementPage(PalletManagementViewModel viewModel)
|
||
|
{
|
||
|
ViewModel = viewModel;
|
||
|
DataContext = this;
|
||
|
InitializeComponent();
|
||
|
WeakReferenceMessenger.Default.Register<PalletManagementViewModel.SavePalletCompletedMessage>(this, (r, m) =>
|
||
|
{
|
||
|
// 提交当前编辑并取消编辑模式
|
||
|
PalletsDataGrid.CommitEdit();
|
||
|
PalletsDataGrid.CancelEdit();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
private void PalletsDataGrid_AddingNewItem(object sender, AddingNewItemEventArgs e)
|
||
|
{
|
||
|
// 确保新项已添加到集合
|
||
|
Dispatcher.BeginInvoke(new Action(() =>
|
||
|
{
|
||
|
if (PalletsDataGrid.Items.Count == 0) return;
|
||
|
|
||
|
var lastIndex = PalletsDataGrid.Items.Count - 1;
|
||
|
PalletsDataGrid.ScrollIntoView(PalletsDataGrid.Items[lastIndex]);
|
||
|
PalletsDataGrid.SelectedIndex = lastIndex;
|
||
|
|
||
|
if (PalletsDataGrid.ItemContainerGenerator.ContainerFromIndex(lastIndex) is not DataGridRow row) return;
|
||
|
row.Focus();
|
||
|
PalletsDataGrid.CurrentColumn = PalletsDataGrid.Columns[0];
|
||
|
PalletsDataGrid.BeginEdit();
|
||
|
}), DispatcherPriority.ContextIdle);
|
||
|
}
|
||
|
|
||
|
// private void PalletDataGrid_RowEditEnding(object? sender, DataGridRowEditEndingEventArgs e)
|
||
|
// {
|
||
|
// if (e.EditAction != DataGridEditAction.Commit) return;
|
||
|
// var dataGrid = (DataGrid)sender;
|
||
|
// dataGrid.CommitEdit(DataGridEditingUnit.Row, true);
|
||
|
// }
|
||
|
|
||
|
private void PalletDataGrid_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;
|
||
|
}
|
||
|
}
|