WPF-如何在单击后隐藏下拉菜单

pol*_*ఠ_ఠ 5 c# wpf split-button drop-down-menu wpf-extended-toolkit

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

<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>
Run Code Online (Sandbox Code Playgroud)

它会生成这样的东西:

测试

问题是,当我单击每个RadioButtons 时,下拉菜单不会消失。我做了一些谷歌搜索,意识到我应该为Click每个RadioButton. 但我不知道如何隐藏该事件处理程序中的下拉菜单。顺便说一句, a 似乎MenuItem 具有 的属性 StaysOpenOnClick,但其他控件没有这样的属性。

虽然以编程方式完成此操作就足够了,但是有 MVVM 方法吗?

M. *_*cki 5

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

沙姆尔

<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>
Run Code Online (Sandbox Code Playgroud)

。CS

 private void rb_Checked(object sender, RoutedEventArgs e)
        {
            SplitButton.IsOpen = false;
        }
Run Code Online (Sandbox Code Playgroud)