修复Menu子菜单概率无法点击,添加Tabs点击标签跟踪Menu选中

This commit is contained in:
czz_y 2025-01-16 10:45:12 +08:00
parent 5f5e6603f4
commit 30c19f8679
2 changed files with 46 additions and 8 deletions

13
MainWindow.Designer.cs generated
View File

@ -130,10 +130,10 @@
this.tabs.Cursor = System.Windows.Forms.Cursors.Default; this.tabs.Cursor = System.Windows.Forms.Cursors.Default;
this.tabs.Dock = System.Windows.Forms.DockStyle.Fill; this.tabs.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabs.Gap = 20; this.tabs.Gap = 20;
this.tabs.Location = new System.Drawing.Point(50, 40); this.tabs.Location = new System.Drawing.Point(70, 40);
this.tabs.Name = "tabs"; this.tabs.Name = "tabs";
this.tabs.Pages.Add(this.tabPage); this.tabs.Pages.Add(this.tabPage);
this.tabs.Size = new System.Drawing.Size(974, 560); this.tabs.Size = new System.Drawing.Size(954, 560);
styleCard2.Closable = true; styleCard2.Closable = true;
this.tabs.Style = styleCard2; this.tabs.Style = styleCard2;
this.tabs.TabIndex = 9; this.tabs.TabIndex = 9;
@ -146,7 +146,7 @@
this.tabPage.Location = new System.Drawing.Point(3, 40); this.tabPage.Location = new System.Drawing.Point(3, 40);
this.tabPage.Name = "tabPage"; this.tabPage.Name = "tabPage";
this.tabPage.ReadOnly = true; this.tabPage.ReadOnly = true;
this.tabPage.Size = new System.Drawing.Size(968, 517); this.tabPage.Size = new System.Drawing.Size(948, 517);
this.tabPage.TabIndex = 1; this.tabPage.TabIndex = 1;
this.tabPage.Text = "首页"; this.tabPage.Text = "首页";
// //
@ -157,7 +157,7 @@
this.panel_content.Location = new System.Drawing.Point(0, 0); this.panel_content.Location = new System.Drawing.Point(0, 0);
this.panel_content.Name = "panel_content"; this.panel_content.Name = "panel_content";
this.panel_content.Radius = 0; this.panel_content.Radius = 0;
this.panel_content.Size = new System.Drawing.Size(968, 517); this.panel_content.Size = new System.Drawing.Size(948, 517);
this.panel_content.TabIndex = 4; this.panel_content.TabIndex = 4;
// //
// menu // menu
@ -168,9 +168,10 @@
this.menu.IconRatio = 1F; this.menu.IconRatio = 1F;
this.menu.Indent = true; this.menu.Indent = true;
this.menu.Location = new System.Drawing.Point(0, 40); this.menu.Location = new System.Drawing.Point(0, 40);
this.menu.Margin = new System.Windows.Forms.Padding(3, 3, 0, 3);
this.menu.Name = "menu"; this.menu.Name = "menu";
this.menu.Padding = new System.Windows.Forms.Padding(6); this.menu.Padding = new System.Windows.Forms.Padding(6, 6, 0, 6);
this.menu.Size = new System.Drawing.Size(50, 560); this.menu.Size = new System.Drawing.Size(70, 560);
this.menu.TabIndex = 8; this.menu.TabIndex = 8;
this.menu.Unique = true; this.menu.Unique = true;
// //

View File

@ -6,6 +6,7 @@ using AntdUIDemo.Views;
using AntdUIDemo.Views.SubView; using AntdUIDemo.Views.SubView;
using Microsoft.Win32; using Microsoft.Win32;
using System; using System;
using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
namespace AntdUIDemo namespace AntdUIDemo
@ -13,6 +14,7 @@ namespace AntdUIDemo
public partial class MainWindow : AntdUI.Window public partial class MainWindow : AntdUI.Window
{ {
private UserControl currControl; private UserControl currControl;
private bool isUpdatingTabs = false;
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
@ -40,10 +42,42 @@ namespace AntdUIDemo
input_search.TextChanged += Input_search_textchanged; input_search.TextChanged += Input_search_textchanged;
menu.SelectChanged += Menu_SelectChanged; menu.SelectChanged += Menu_SelectChanged;
button_collapse.Click += button_collapse_Click; button_collapse.Click += button_collapse_Click;
tabs.SelectedIndexChanged += Tabs_SelectedIndexChanged;
//监听系统深浅色变化 //监听系统深浅色变化
SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged; SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
} }
private void Tabs_SelectedIndexChanged(object sender, IntEventArgs e)
{
if (isUpdatingTabs) return;
var text = tabs.SelectedTab?.Text; // 使用安全导航操作符,防止 SelectedTab 为 null
if (string.IsNullOrEmpty(text)) // 检查 text 是否为 null 或空
{
return; // 如果 text 为空,直接退出方法
}
//首页
if (text == "首页")
{
return;
}
var rootIndex = 0;
var subIndex = 0;
var menuItemsCopy = menu.Items.ToList(); // 创建副本
for (int i = 0; i < menuItemsCopy.Count; i++)
{
for (int j = 0; j < menuItemsCopy[i].Sub.Count; j++)
{
if (menuItemsCopy[i].Sub[j].Tag.ToString() == text)
{
rootIndex = i;
subIndex = j;
break;
}
}
}
menu.SelectIndex(rootIndex, subIndex, true);
}
private void button_collapse_Click(object sender, EventArgs e) private void button_collapse_Click(object sender, EventArgs e)
{ {
if (menu.Collapsed) if (menu.Collapsed)
@ -52,7 +86,7 @@ namespace AntdUIDemo
} }
else else
{ {
menu.Width = (int)(50 * Config.Dpi); menu.Width = (int)(70 * Config.Dpi);
} }
button_collapse.Toggle = !button_collapse.Toggle; button_collapse.Toggle = !button_collapse.Toggle;
menu.Collapsed = !menu.Collapsed; menu.Collapsed = !menu.Collapsed;
@ -385,12 +419,15 @@ namespace AntdUIDemo
//容器添加控件需要调整dpi //容器添加控件需要调整dpi
control.Dock = DockStyle.Fill; control.Dock = DockStyle.Fill;
AutoDpi(control); AutoDpi(control);
AntdUI.TabPage tabPage = new AntdUI.TabPage() { AntdUI.TabPage tabPage = new AntdUI.TabPage()
{
Text = name, Text = name,
}; };
tabPage.Controls.Add(control); tabPage.Controls.Add(control);
tabs.Pages.Add(tabPage); tabs.Pages.Add(tabPage);
isUpdatingTabs = true;
tabs.SelectedTab = tabPage; tabs.SelectedTab = tabPage;
isUpdatingTabs = false;
currControl = control; currControl = control;
} }
} }