1

In WPF I want to have a button that when clicked, it either opens or closes a Popup depending on whether or not it's already open (Close it if it's open, Open it if it's closed), and I want to do this purely in XAML. Is this possible?

Thanks,
Roy

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
LPCRoy
  • 935
  • 2
  • 12
  • 19

3 Answers3

6

Yes, you will need to use a ToggleButton rather than a standard button. Then you should be able to bind the Popup.IsOpen property to the ToggleButton.IsChecked property.

If you need a XAML snippet demonstrating this, I can make one in a couple minutes. But this should be straightforward enough.

Charlie
  • 15,069
  • 3
  • 64
  • 70
  • Here is an example of this technique: http://stackoverflow.com/questions/361209/how-to-open-a-wpf-popup-when-another-control-is-clicked-using-xaml-markup-only/399824#399824 – Helge Klein Apr 14 '11 at 18:36
1

Here is a nice solution to the question implemented as a behavior and thus independent of code behind / the ViewModel.

Helge Klein
  • 8,829
  • 8
  • 51
  • 71
0

I just solve this problem, just as @Charlie said. here is my code in a page.xaml file.

<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>
Annly Yang
  • 21
  • 1