【问题标题】:WPF- How to hide a dropdown menu after clickWPF-单击后如何隐藏下拉菜单
【发布时间】:2016-09-27 06:48:03
【问题描述】:

我的 WPF 窗口中有一个 SplitButton,它是从 Xceed 的扩展 WPF 工具包中借来的。它的下拉内容由一些RadioButtons 组成。比如:

<Window x:Class="WpfTest.Test3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:tk="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"
        Title="Test3" Height="300" Width="300">
    <Grid Height="25" Width="150">
        <tk:SplitButton Content="Default Command">
            <tk:SplitButton.DropDownContent>
                <StackPanel>
                    <RadioButton Content="Default Command" GroupName="variations" Margin="5" IsChecked="True"/>
                    <RadioButton Content="Alternate Command 1" GroupName="variations" Margin="5"/>
                    <RadioButton Content="Alternate Command 2" GroupName="variations" Margin="5"/>
                </StackPanel>
            </tk:SplitButton.DropDownContent>
        </tk:SplitButton>
    </Grid>
</Window>

它会生成这样的东西:

问题是,当我点击每个RadioButtons 时,下拉菜单并没有消失。我做了一些谷歌搜索并意识到我应该为每个RadioButton 处理Click 事件。但我不知道如何隐藏该事件处理程序中的下拉菜单。作为旁注,它似乎是MenuItemhas the property ofStaysOpenOnClick,但其他控件则没有。

虽然以编程方式执行此操作就足够了,但有没有 MVVM 方法呢?

【问题讨论】:

  • 不完全是您问题的解决方案,但为什么您更喜欢单选按钮而不是下拉列表?
  • @Zure 什么是下拉列表? SplitButton 有一个 DropDownContent 属性,可以用MenuItems 或类似我尝试过的东西填充。我没明白你的意思
  • 对不起,我的意思是一个组合框:dotnetperls.com/combobox-wpf

标签: c# wpf drop-down-menu split-button wpf-extended-toolkit


【解决方案1】:

在您的单选按钮上添加 Checked 事件并使用SplitoButton.IsOpen=false;。遵循此代码。

Xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:tk="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <tk:SplitButton Name="SplitButton" Content="Default Command">

            <tk:SplitButton.DropDownContent>

                <StackPanel>
                    <RadioButton Checked="rb_Checked" Content="Default Command" GroupName="variations" Margin="5" IsChecked="True"/>
                    <RadioButton Checked="rb_Checked" Content="Alternate Command 1" GroupName="variations" Margin="5"/>
                    <RadioButton Checked="rb_Checked" Content="Alternate Command 2" GroupName="variations" Margin="5"/>
                </StackPanel>

            </tk:SplitButton.DropDownContent>
        </tk:SplitButton>
    </Grid>
</Window>

.cs

 private void rb_Checked(object sender, RoutedEventArgs e)
        {
            SplitButton.IsOpen = false;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 2015-10-11
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多