2012-03-01 52 views
0

我想在IsChecked設置爲true時打開ToggleButton.ContextMenu。當IsChecked屬性將狀態更改爲True時,在ToggleButton上打開ContextMenu

我下面寫的代碼,但是ContextMenu.IsOpen沒有改變:

<ToggleButton x:Name="btnRegularButton"    
         Content="Regular Button"> 

      <ToggleButton.Style> 
       <Style TargetType="ToggleButton"> 
        <Style.Triggers> 

         <Trigger Property="IsChecked" Value="True"> 
          <Setter Property="ContextMenu.IsOpen" Value="True" /> 
         </Trigger> 

        </Style.Triggers> 
       </Style> 
      </ToggleButton.Style> 

      <ToggleButton.ContextMenu> 

       <ContextMenu>              
        <MenuItem Header="Save" /> 
        <MenuItem Header="Print" /> 
        <Separator /> 
        <MenuItem Header="Exit" /> 
       </ContextMenu> 
      </ToggleButton.ContextMenu> 

     </ToggleButton> 

回答

0
<Window x:Class="Staticoverflow.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
    <ContextMenu x:Key="ContextMenu1"> 
     <MenuItem Header="Save" /> 
     <MenuItem Header="Print" /> 
     <Separator /> 
     <MenuItem Header="Exit" /> 
    </ContextMenu> 
</Window.Resources> 
<Grid> 
    <Grid.RowDefinitions > 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <ToggleButton x:Name="btnRegularButton"    
        Content="Regular Button"> 

     <ToggleButton.Style> 
      <Style TargetType="ToggleButton"> 
       <Style.Triggers> 
        <Trigger Property="IsChecked" Value="True"> 
         <Setter Property="ContextMenu" Value="{StaticResource ContextMenu1}" /> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </ToggleButton.Style> 


    </ToggleButton> 

</Grid> 

希望這會有所幫助。

+0

不幸的是,這不起作用 – f4x 2012-03-02 01:41:48

+2

爲什麼它被標記爲答案呢? – 2015-06-12 13:33:09

相關問題