wpf WPF拆分按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1413536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:59:26  来源:igfitidea点击:

WPF SplitButton?

wpfbuttonsplit-button

提问by David Veeneman

I've just spent a very frustrating afternoon trawling Google looking for a commercial-grade WPF SplitButtoncontrol that will work in a ToolBar. A SplitButtonis one where you can click on the main part of the Buttonto take a default action, or click on a little triangle on the right to get a drop-down menu of alternate actions).

我刚刚花了一个非常令人沮丧的下午在 Google 上搜索,寻找可以SplitButtonToolBar. ASplitButton是您可以单击 的主要部分Button以执行默认操作,或单击右侧的小三角形以获取备用操作的下拉菜单的地方)。

I found several on the web (including the one on CodeProject, and including the two on CodePlex). None of them work properly in a ToolBar--they either don't appear at all, or they don't have toolbar button styling. I even looked at some commercial offerings, like ActiPro's pop-up button (in their SharedLibrary DLL). Same problems.

我在网上找到了几个(包括 CodeProject 上的一个,以及 CodePlex 上的两个)。它们ToolBar都不能正常工作 -- 它们要么根本不出现,要么没有工具栏按钮样式。我什至查看了一些商业产品,比如 ActiPro 的弹出按钮(在他们的 SharedLibrary DLL 中)。同样的问题。

And yes, I've seen all the posts about how easy it is to create one. It's very easy to create a bad one, but not so easy to create one that looks and works like the SplitButtons in Outlook or Visual Studio, where the drop-down menu doesn't disappear if you release the mouse button.

是的,我已经看过所有关于创建一个是多么容易的帖子。创建一个坏的很容易,但要创建一个看起来和工作方式都像SplitButtonOutlook 或 Visual Studio 中的s 的 s 就不是那么容易了,如果释放鼠标按钮,下拉菜单不会消失。

So, here's my question: Are there any commercial-grade SplitButtons out there, either open-source or commercial, that work in toolbars? I'm not looking for a control that is part of a $1,500 annual subscription to somebody's controls library, but if there is a reasonably-priced SplitButton, I'd sure like to find it.

所以,这是我的问题:是否有任何商业级别SplitButton的工具,无论是开源的还是商业的,可以在工具栏中使用?我不是在寻找一个控件,它是每年 1,500 美元订阅某人控件库的一部分,但如果有一个价格合理的SplitButton,我肯定会找到它。

采纳答案by Reed Copsey

The only true "commercial-grade" split button that I know of is the one by Syncfusion, which is included as part of their ribbon controls (although it works outside of the ribbon as well).

我所知道的唯一真正的“商业级”拆分按钮是Syncfusion的一个,它作为其功能区控件的一部分包含在内(尽管它也可以在功能区之外使用)。

That being said, I remember this implementationas being fairly usable and complete, if you're looking for something free.

话虽如此,我记得这个实现是相当可用和完整的,如果你正在寻找免费的东西。

回答by Alex_P

Another good free implementation that seem to have it all:

另一个很好的免费实现,似乎拥有一切:

http://huydinhpham.blogspot.com/2008/09/wpf-drop-down-and-split-button.html

http://huydinhpham.blogspot.com/2008/09/wpf-drop-down-and-split-button.html

  • The split button can be used in the toolbar and has a proper toolbar style. It can also be restyled if you want.
  • Dropdown menu is exposed via its own property - i.e splitbutton can have it's own context menu separate from the dropdown one (even though it seems illogical it can be useful in some cases - like toolbar context menu that pops up when you right click on a button placed in the toolbar).
  • The dropdown menu is standard ContextMenu - i.e. content can be databound, menu items restyled etc.
  • Both the main and the dropdown parts of the split button have command properties associated with them.
  • 拆分按钮可以在工具栏中使用,并具有适当的工具栏样式。如果你愿意,它也可以重新设计。
  • 下拉菜单通过其自己的属性公开 - 即 splitbutton 可以将它自己的上下文菜单与下拉菜单分开(即使它看起来不合逻辑,但它在某些情况下可能很有用 - 例如右键单击按钮时弹出的工具栏上下文菜单放在工具栏中)。
  • 下拉菜单是标准的 ContextMenu - 即内容可以是数据绑定的,菜单项重新样式等。
  • 拆分按钮的主要部分和下拉部分都有与之关联的命令属性。

回答by huseyint

There is a pretty good split button implementation for WPF and Silverlight over Delay's blog:

Delay 的博客上有一个很好的 WPF 和 Silverlight 拆分按钮实现:

Banana SplitButton (A WPF-specific fix for SplitButton and some code analysis improvements for the Silverlight version, too)

Banana SplitButton(SplitButton 的 WPF 特定修复以及 Silverlight 版本的一些代码分析改进)

回答by Benoit Blanchon

The Extended WPF Toolkit Community Edition(which is free) has a nice SplitButton(and it has a DropDownButtonas well)

扩展WPF工具包社区版(这是免费的),有一个很好的SplitButton(它有一个DropDownButton为好)

SplitButton screen capture

SplitButton 屏幕截图

<xctk:SplitButton Content="Click Me">
    <xctk:SplitButton.DropDownContent>
        <xctk:ColorCanvas />
    </xctk:SplitButton.DropDownContent>
 </xctk:SplitButton>

回答by Anderson Imes

I don't know what exactly you are looking for in a split button, but this video on how to create one is pretty complete and makes a splitbutton that is just about perfect.

我不知道您在拆分按钮中究竟要寻找什么,但是这个关于如何创建拆分按钮的视频非常完整,并且制作了一个几乎完美的拆分按钮。

http://windowsclient.net/learn/video.aspx?v=3929

http://windowsclient.net/learn/video.aspx?v=3929

I know you didn't want a tutorial, but I've used this before and you couldn't tell the difference between it and the ones in outlook.

我知道你不想要教程,但我以前用过这个,你看不出它和 Outlook 中的区别。

回答by codymanix

I think what you mean is called a DropDownButton. There is a boolean property on MenuItem "StaysOpenOnClick" which could solve your problem.

我认为你的意思是所谓的 DropDownButton。MenuItem“StaysOpenOnClick”上有一个布尔属性可以解决您的问题。

回答by KornMuffin

I was looking for the same and just rolled my own (you will need to style to your liking (to match the ToolBar) and you could refactor it / convert it into a custom control ... etc.)

我一直在寻找相同的东西,只是推出了我自己的(您需要根据自己的喜好设置样式(以匹配工具栏),并且您可以重构它/将其转换为自定义控件......等)

<StackPanel x:Name="Split" Orientation="Horizontal">
    <Button Command="{Binding MainCommand}">
        <StackPanel>
            <Image Source="{StaticResource MainCommandImage}"/>
            <TextBlock>MainCommand</TextBlock>
        </StackPanel>
    </Button>
    <Separator HorizontalAlignment="Left" Width="1" VerticalAlignment="Stretch" Margin="0,5"/>
    <CheckBox Width="16" IsThreeState="False">
        <Grid>
            <Path Fill="Black" Data="{StaticResource DownArrowGeometry}"
                  Stretch="Uniform" Height="6" Width="6" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <Popup x:Name="popupOptions" AllowsTransparency="True" PopupAnimation="Fade" StaysOpen="False" 
                   Placement="Bottom" PlacementTarget="{Binding ElementName=Split}" HorizontalOffset="-3"
                   IsOpen="{Binding RelativeSource={RelativeSource AncestorType={x:Type CheckBox}, AncestorLevel=1}, Path=IsChecked}">
                <StackPanel>
                    <StackPanel>
                        <Image Source="{StaticResource SubCommandImage1}"/>
                        <TextBlock>SubCommand1</TextBlock>
                     </StackPanel>
                    <StackPanel>
                        <Image Source="{StaticResource SubCommandImage2}"/>
                        <TextBlock>SubCommand2</TextBlock>
                     </StackPanel>
                </StackPanel>
            </Popup>
        </Grid>
    </CheckBox>
</StackPanel>

回答by d347hm4n

Using the WPF Toolkit split button to show a context menu is reasonably straight forward. Add a context menu in your window resources. On the window load - bind the context menu to the split button and then use the context menu as you would do normally.

使用 WPF Toolkit 拆分按钮显示上下文菜单相当简单。在您的窗口资源中添加上下文菜单。在窗口加载时 - 将上下文菜单绑定到拆分按钮,然后像往常一样使用上下文菜单。

It really needs to be added in the WPF Toolkit as the majority use case for this button is to replicate the old WinForm Splitt button.

它确实需要添加到 WPF 工具包中,因为此按钮的主要用例是复制旧的 WinForm Splitt 按钮。

enter image description here

在此处输入图片说明

<Window x:Class="SplitButtonTesting.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ContextMenu x:Key="contextMenu" IsOpen="{Binding IsOpen}">
        <MenuItem Header="One" />
        <MenuItem Header="Two" />
        <MenuItem Header="More...">
            <MenuItem Header="One" />
            <MenuItem Header="Two" />
        </MenuItem>
    </ContextMenu>
</Window.Resources>
<DockPanel>

    <Menu DockPanel.Dock="Top" x:Name="ApplicationMenu">

        <xctk:SplitButton x:Name="SplitButton" Content="Main Button" DropDownContent="{x:Null}" />

    </Menu>
    <Border />

</DockPanel>

Code behind:

后面的代码:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace SplitButtonTesting
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            SetupSplitButton();
        }

        public void SetupSplitButton()
       {
           var menu = this.Resources["contextMenu"] as ContextMenu;

           menu.PlacementTarget = SplitButton;

            menu.Placement = PlacementMode.Bottom;
            menu.DataContext = SplitButton;
        }
    }
}