如何在wpf中创建停靠(浮动)工具栏

发布于 2024-09-03 01:55:29 字数 170 浏览 1 评论 0原文

我是 wpf 新手。我必须在 wpf 中创建一个浮动ToolBar,就像 ms - office 2003 工具栏一样。 这样我就可以将它放置在从上到下、从左到右的任何位置,就像在 Office 2003 中一样。

请帮助我..............................

I am new to wpf. I have to create a floating ToolBar in wpf like the ms - office 2003 toolbar.
So that I can place it anywhere top - bottom, left- right as same as it was in office 2003.

Please help me .......................

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-09-10 01:55:30

要创建工具栏并向其添加按钮,此代码可能会帮助您...

 Toolbar m = new ToolBar();

//创建名称为 m 的工具栏

// 您可以设置更多属性

m.Divider = true;
m.DropDownArrows = true;
m.Size = new System.Drawing.Size(250, 25);
m.Wrappable = true;

ToolBarButton tb1 = new ToolBarButton();
ToolBarButton tb2 = new ToolBarButton();
ToolBarButton tb3 = new ToolBarButton();

tb1.Text = "Admin";
tb2.Text = "Teacher";
tb3.Text = "Student";

m.Buttons.Add(tb1);
m.Buttons.Add(tb2);
m.Buttons.Add(tb3);
Controls.Add(m);

private void m_Clicked(Object sender,
                        ToolBarButtonClickEventArgs e)
{

switch (m.Buttons.IndexOf(e.Button))
    {
        case 1:
            MessageBox.Show("Admin logged in");
            break;
        case 2:
             MessageBox.Show("Teacher logged");            
            break;
        case 3:MessageBox.Show("Student loged in");            
            break;
                case 4:
            this.Close();   
            break;
}
}

to create tool bar and add buttons to it, this code may help you...

 Toolbar m = new ToolBar();

//creates toolbar with name m

// you can set som more properties

m.Divider = true;
m.DropDownArrows = true;
m.Size = new System.Drawing.Size(250, 25);
m.Wrappable = true;

ToolBarButton tb1 = new ToolBarButton();
ToolBarButton tb2 = new ToolBarButton();
ToolBarButton tb3 = new ToolBarButton();

tb1.Text = "Admin";
tb2.Text = "Teacher";
tb3.Text = "Student";

m.Buttons.Add(tb1);
m.Buttons.Add(tb2);
m.Buttons.Add(tb3);
Controls.Add(m);

private void m_Clicked(Object sender,
                        ToolBarButtonClickEventArgs e)
{

switch (m.Buttons.IndexOf(e.Button))
    {
        case 1:
            MessageBox.Show("Admin logged in");
            break;
        case 2:
             MessageBox.Show("Teacher logged");            
            break;
        case 3:MessageBox.Show("Student loged in");            
            break;
                case 4:
            this.Close();   
            break;
}
}
攒眉千度 2024-09-10 01:55:29

对于简单的对接,您可以使用 DockPanel

<DockPanel>
    <Button DockPanel.Dock="Top">This would be a toolbar at the top</Button>
    <Butto>This would the main work area</Button>
</DockPanel>

<DockPanel>
    <Button DockPanel.Dock="Left">This would be a toolbar at the left</Button>
    <Button>This would the main work area</Button>
</DockPanel>

您当然可以使用更适合您需求的类来代替 Button。

然而,当您需要一个带有浮动窗口的窗口系统时,您将不得不恢复到第 3 方库,因为 WPF 没有它,并且很难推出自己的库。以下是一些库:

如果您真正需要的是停靠浮动工具栏(而不是其他窗口),您可以使用 ToolBar 类ToolBarTray 类。但是您需要编写代码来检测拖动,从可视化树中删除 ToolBar 元素,然后将其作为根可视化添加到您自己的 Window 或 HwndSource。然后,您需要检测窗口何时位于放置区域上方,以将工具栏从窗口移动到主窗口的可视化树并关闭另一个窗口。

For the plain docking you would use the DockPanel:

<DockPanel>
    <Button DockPanel.Dock="Top">This would be a toolbar at the top</Button>
    <Butto>This would the main work area</Button>
</DockPanel>

<DockPanel>
    <Button DockPanel.Dock="Left">This would be a toolbar at the left</Button>
    <Button>This would the main work area</Button>
</DockPanel>

Instead of Button you would of course use the classes that are more apropriate for your needs.

However when you needs a windowing system with floating windows you will have to revert to a 3rd party library because it WPF does not have it and it would be pretty hard to roll your own. Here are some libs:

If all you really need is the docking floating toolbar (and no other windows) you can use the ToolBar class in conjunction with the ToolBarTray class. But you will need to write code to detect the drag, remove the ToolBar element from the visual tree, and then add it as a root visual to your own Window or HwndSource. You'll then need to detect when the window is over your drop zone to move the ToolBar from the window to the main window's visual tree and close the other window.

紫瑟鸿黎 2024-09-10 01:55:29

我建议您查看第三方控件库。 Syncfusion 是一款商业产品,其基本工具集合中包含停靠管理器组件。但它并不真的像 Office 2k3(更像 Visual Studio)。 codeplex 上还有一个 ,我确信还有其他几个各种价格范围。

对于从主工具栏区域实际取消工具栏的停靠,我相信 标准 WPF 工具栏< /a> 控件已经支持这一点。至少您可以在工具栏托盘中移动它们< /a>.

I recommend you look at third party control libraries for this. Syncfusion is a commercial product that contains a dock manager component in their essential tools collection. It's not really like office 2k3 though (more like Visual Studio). There is also one on codeplex and I'm sure there are several others at various price ranges.

For the actual undocking of toolbars from the main toolbar area I believe the standard WPF toolbar controls already supports this. At least you can move them around within a toolbar tray.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文