【发布时间】:2009-07-29 20:37:38
【问题描述】:
在 WPF 中,我希望有一个按钮,当单击它时,它会打开或关闭弹出窗口,具体取决于它是否已经打开(如果它打开则关闭它,如果它关闭则打开它),我想这样做纯粹在 XAML 中。这可能吗?
谢谢,
罗伊
【问题讨论】:
在 WPF 中,我希望有一个按钮,当单击它时,它会打开或关闭弹出窗口,具体取决于它是否已经打开(如果它打开则关闭它,如果它关闭则打开它),我想这样做纯粹在 XAML 中。这可能吗?
谢谢,
罗伊
【问题讨论】:
是的,您需要使用ToggleButton 而不是标准按钮。然后您应该能够将 Popup.IsOpen 属性绑定到 ToggleButton.IsChecked 属性。
如果您需要一个 XAML sn-p 来演示这一点,我可以在几分钟内制作一个。但这应该足够简单。
【讨论】:
这是作为行为实现的问题的nice solution,因此独立于 ViewModel 后面的代码。
【讨论】:
正如@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>
【讨论】: