在一个文件管理项目中需要做一个收藏夹时,如果用普通的Window时需要处理点按钮弹出,点无关的地方再自动关闭。也可以做就是比较麻烦。WPF自带提供了一种弹出窗体Popup。可以很方便实现这些功能。

XML
<ToggleButton Style="{StaticResource ToggleImageStyle}"
zhao:IconElement.Width="20"
zhao:IconElement.Height="20"
zhao:IconElement.Image="{StaticResource 收藏}"
Command="{Binding Ok2Command}">
<Popup VerticalOffset="2" Focusable="False" StaysOpen="False"
AllowsTransparency="True" PopupAnimation="Slide"
MinWidth="200" MaxWidth="400" MinHeight="100" MaxHeight="600"
Placement="Bottom" PlacementTarget="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ToggleButton}}"
IsOpen="{Binding IsChecked, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ToggleButton}}">
<Border Background="White" BorderBrush="Gray" BorderThickness="1.5" CornerRadius="5" Margin="10">
<Border.Effect>
<DropShadowEffect Color="Gray" ShadowDepth="3" BlurRadius="5" Opacity="0.3"/>
</Border.Effect>
<Grid Margin="3">
<TextBlock Text="我是一个Popup" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Border>
</Popup>
</ToggleButton>