2016-09-27 104 views
2

我有一个SplitButton在我的WPF窗口,这是从Xceed的扩展WPF工具包借来的。其下拉内容由一些RadioButton组成。喜欢的东西:WPF-如何隐藏下拉菜单后点击

<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> 

产生这样的:

test

的问题是,当我点击每个RadioButton S中的下拉菜单不dissappear的。我做了一些Google搜索,意识到我应该为每个RadioButton处理Click事件。但我不知道如何隐藏该事件处理程序中的下拉菜单。作为一个侧面说明,它似乎是一个MenuItemhas the property ofStaysOpenOnClick,但没有其他控件的这种事情。

尽管以编程方式完成此操作就足够了,但有没有MVVM的方式?

+1

不完全是你的问题的解决方案,但你为什么宁可单选按钮在下拉列表? –

+0

@确定什么是下拉列表? 'SplitButton'有一个'DropDownContent'属性,可以用'MenuItem'或者我尝试的那个来填充。我没有明白你的意思 –

+0

对不起,我的意思是一个组合框:https://www.dotnetperls.com/combobox-wpf –

回答

1

在您的单选按钮上添加Checked event并使用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; 
     }