LPCRoy
LPCRoy

Reputation: 945

WPF Popup Button Question

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

Upvotes: 1

Views: 3866

Answers (3)

Annly Yang
Annly Yang

Reputation: 21

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>

Upvotes: 0

Helge Klein
Helge Klein

Reputation: 9085

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

Upvotes: 1

Charlie
Charlie

Reputation: 15247

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.

Upvotes: 6

Related Questions