123 lines
4.6 KiB
C#
123 lines
4.6 KiB
C#
using MembranePoreTester.ViewModels;
|
||
using System.Windows;
|
||
using System.Windows.Controls;
|
||
using System.Windows.Input;
|
||
using System.Windows.Media;
|
||
|
||
namespace MembranePoreTester.Views
|
||
{
|
||
public partial class MainWindow : Window
|
||
{
|
||
public MainWindow()
|
||
{
|
||
InitializeComponent();
|
||
HistoryWindow.LoadRecordEvent += OnLoadRecord;
|
||
Loaded += MainWindow_Loaded;
|
||
}
|
||
|
||
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
|
||
{
|
||
// 根据角色设置权限
|
||
bool isAdmin = App.CurrentUserRole == Models.UserRole.Admin;
|
||
|
||
// 禁用/启用阀门控制按钮(假设 x:Name="btnValveControl")
|
||
if (btnValveControl != null) btnValveControl.IsEnabled = isAdmin;
|
||
//// 如果还有参数设置按钮(例如工具栏上的按钮)
|
||
//if (btnParameterSetting != null) btnParameterSetting.IsEnabled = isAdmin;
|
||
//// 其他需要限制的全局控件可在此添加
|
||
}
|
||
|
||
private void OnLoadRecord(object sender, LoadRecordEventArgs e)
|
||
{
|
||
var mainVM = DataContext as MainViewModel;
|
||
if (mainVM == null) return;
|
||
|
||
// 根据工位和类型找到对应的 ViewModel
|
||
if (e.Type == "泡点法")
|
||
{
|
||
var targetVM = mainVM.Stations[e.TargetStation - 1].BubblePointVM;
|
||
targetVM.LoadFromDatabase(e.RecordId);
|
||
}
|
||
else
|
||
{
|
||
var targetVM = mainVM.Stations[e.TargetStation - 1].PoreDistributionVM;
|
||
targetVM.LoadFromDatabase(e.RecordId);
|
||
}
|
||
}
|
||
|
||
private void OpenHistory_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
var mainVM = DataContext as MainViewModel;
|
||
if (mainVM == null) return;
|
||
|
||
int currentStation = stationTabControl.SelectedIndex + 1;
|
||
var historyWin = new HistoryWindow { SelectedStation = currentStation };
|
||
historyWin.ShowDialog();
|
||
}
|
||
|
||
private void Window_KeyDown(object sender, KeyEventArgs e)
|
||
{
|
||
// 仅管理员可使用 Ctrl+P 打开参数设置窗口
|
||
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.P)
|
||
{
|
||
if (App.CurrentUserRole == Models.UserRole.Admin)
|
||
{
|
||
var win = new ParameterWindow();
|
||
win.Owner = this;
|
||
win.ShowDialog();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("权限不足,无法打开参数设置", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||
}
|
||
}
|
||
}
|
||
|
||
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||
{
|
||
var selectedStation = stationTabControl.SelectedItem as MainViewModel.StationItem;
|
||
if (selectedStation != null)
|
||
{
|
||
var content = stationTabControl.SelectedContent as ContentPresenter;
|
||
if (content != null)
|
||
{
|
||
var innerTabControl = FindVisualChild<TabControl>(content);
|
||
if (innerTabControl != null)
|
||
{
|
||
var selectedTabItem = innerTabControl.SelectedItem as TabItem;
|
||
bool isPoreDistributionActive = selectedTabItem?.Header?.ToString() == "孔分布测试";
|
||
|
||
selectedStation.IsPoreDistributionActive = isPoreDistributionActive;
|
||
selectedStation.PoreDistributionVM.IsActive = isPoreDistributionActive;
|
||
|
||
// 清空数据和停止采集的代码已注释,保持原样
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
|
||
{
|
||
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
|
||
{
|
||
var child = VisualTreeHelper.GetChild(parent, i);
|
||
if (child is T t) return t;
|
||
var result = FindVisualChild<T>(child);
|
||
if (result != null) return result;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
private void OpenValveControl_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
if (App.CurrentUserRole != Models.UserRole.Admin)
|
||
{
|
||
MessageBox.Show("权限不足,无法打开阀门控制", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||
return;
|
||
}
|
||
var win = new ValveControlWindow();
|
||
win.Owner = this;
|
||
win.Show();
|
||
}
|
||
}
|
||
} |