2009-07-29 147 views
1

在WPF中,我想要一個按鈕,當它被點擊時,它會打開或關閉一個彈出窗口,具體取決於它是否已經打開(關閉它,如果它打開,打開它,如果它是關閉的),我希望純粹在XAML中完成此操作。這可能嗎?WPF彈出式按鈕問題

謝謝
羅伊

回答

6

是的,你需要使用一個ToggleButton,而不是一個標準的按鈕。然後,您應該能夠將Popup.IsOpen屬性綁定到ToggleButton.IsChecked屬性。

如果您需要一個XAML片段來證明這一點,我可以在幾分鐘內製作一個。但這應該足夠簡單。

+0

下面是這種技術的示例:http://stackoverflow.com/questions/361209/how-to-open-a-wpf-popup-when-another-control-is-clicked-using- XAML的標記只/ 399824#399824 – 2011-04-14 18:36:52

1

這是一個nice solution作爲一個行爲實現的問題,因此獨立於ViewModel之後的代碼。

0

我剛剛解決了這個問題,就像@Charlie說的那樣。 這裏是我在page.xaml文件中的代碼。

<ToggleButton Name="MenuButton" Width="120" Height="40"/> 
      <Popup Width="130" Height="150" PlacementTarget="{Binding ElementName=MenuButton}" Placement="Bottom" AllowsTransparency="True" PopupAnimation="Fade" IsOpen="{Binding ElementName=MenuButton,Path=IsChecked}" StaysOpen="False"> 
       <Border Background="Black" CornerRadius="10"> 
        <Grid > 
         <Grid.RowDefinitions> 
          <RowDefinition /> 
          <RowDefinition /> 
          <RowDefinition /> 
         </Grid.RowDefinitions> 
         <Grid Grid.Row="0"> 
          <Button Content="Log" Width="120" Height="40"/> 
         </Grid> 
         <Grid Grid.Row="1"> 
          <Button Content="Shut Down" Width="120" Height="40"/> 
         </Grid> 
         <Grid Grid.Row="2"> 
          <Button Content="About..." Width="120" Height="40"/> 
         </Grid> 
        </Grid> 
       </Border> 
      </Popup>