2016-09-27 105 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; 
     }