关于WPF中Popup中的一些用法的总结

1.常用属性说明

IsOpen: 布尔值,指示 Popup 控件是否显示

StaysOpen: 布尔值,指示在 Popup 控件失去焦点的时候,是否关闭 Popup 控件的显示

PopupAnimation:指示显示窗口时是否使用动画,只有在 AllowsTransparency 等于true时此属性才有用,设置一些Popup的弹出时的动画效果。我们可以设置PopupAnimation="Fade" 表示弹出时是通过渐入的方式进入的

Popup 窗口本身是一个不可见的元素,只有在窗口上放置了信息后才能显示
Popup的定位方式与一般控件的定位方法不一样, Popup 使用五个属性来设定位置信息:
PlacementTarget:设定 Popup 定义所相对的控件,PlacementTarget="{Binding ElementName= '控件的名字'}"

如果没有为属性为 NULL,则 Popup 定位相对于屏幕的左上角

Placement:一个枚举值,指定 Popup 控件的定位方式

PlacementRectangle:设定一个矩形,在 Popup 控件显示时,位置将相对于此矩形来显示,此矩形的位置也相对于PlacementTarget 属性所设定的控件
HorizontalOffset:指定一个值,指示 Popup 的位置所需水平移动多少个象素
VerticalOffset:指定一个值,指示 Popup 的位置所需垂直移动多少个象素

<!--悬浮菜单-->
<Canvas x:Name="Search2" Background="#303030" Panel.ZIndex="5" Margin="-2,0,0,-2" Grid.Row="0" Visibility="Collapsed">
<ScrollViewer x:Name="ScrollViewer1" VerticalScrollBarVisibility="Hidden" Height="{Binding ElementName=Search2, Path=ActualHeight, Mode=OneWay}" Style="{StaticResource for_scrollviewer}">
<ItemsControl x:Name="itemsControl1" ItemsSource="{Binding Menus}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl >
<Border>
<StackPanel Margin="1,0,0,0">
<Button x:Name="btnIcon" Style="{StaticResource btnMemoryStyle}"/>
</StackPanel>
</Border>
</ContentControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Canvas>

<!--悬浮菜单按钮 使用动画-->
<Style x:Key="btnMemoryStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Resources>
<Storyboard x:Key="Timeline1">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="_Border" Storyboard.TargetProperty="Width">
<SplineDoubleKeyFrame KeyTime="00:00:00.0000000" Value="300"/>
</DoubleAnimationUsingKeyFrames>
<!--<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="pop" Storyboard.TargetProperty="Width">
<SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="200"/>
</DoubleAnimationUsingKeyFrames>-->
</Storyboard>
<Storyboard x:Key="Timeline2">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="_Border" Storyboard.TargetProperty="Width">
<SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<!--<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="pop" Storyboard.TargetProperty="Width">
<SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="0"/>
</DoubleAnimationUsingKeyFrames>-->
</Storyboard>
</ControlTemplate.Resources>
<Border x:Name="bd">
<Grid HorizontalAlignment="Left">
<Border x:Name="_Border" Background="#303030" CornerRadius="0" Height="60" Width="0" Margin="60,0,0,0">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Left">
<StackPanel>
<TextBlock x:Name="TextBlock1" VerticalAlignment="Center" HorizontalAlignment="Left"
Style="{DynamicResource TreeMainItemLabelStyle}" Text="{Binding Title}"/>
</StackPanel>
<Popup x:Name="pop" AllowsTransparency="True" Placement="Bottom" PlacementTarget="{Binding ElementName=TextBlock1}">
<Border CornerRadius="5">
<StackPanel>
<ItemsControl x:Name="itemsControl1" ItemsSource="{Binding Items}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl >
<StackPanel Style="{DynamicResource BackgroundStackPanel2}">
<StackPanel Cursor="Hand" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10" Width="274" >
<TextBlock Style="{DynamicResource FontAweSomeSkin}" Margin="20,5,0,0" Text="{Binding Path=ImagePath,
Converter={StaticResource ConvertFontIconEncode}}" FontSize="{DynamicResource TreeSubItemLabelFontSize}"
VerticalAlignment="Center" TextAlignment="Center" Width="25"/>
<TextBlock Style="{DynamicResource TreeMainItemLabelStyle}" Margin="20,5,0,0" Text="{Binding Title,Converter={StaticResource StringFormatConvert},ConverterParameter=15}"
VerticalAlignment="Center"/>
<StackPanel.InputBindings>
<MouseBinding MouseAction="LeftClick" Command="{Binding DataContext.NavigateLinkCommand,RelativeSource={RelativeSource AncestorType=UserControl}}"
CommandParameter="{Binding}"/>
</StackPanel.InputBindings>
</StackPanel>
</StackPanel>
</ContentControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Border>
</Popup>
</StackPanel>
</Border>
<Grid HorizontalAlignment="Left">
<Rectangle x:Name="Rectangle1" Fill="#303030" Height="60" Width="60"/>
<TextBlock x:Name="txtIco" Style="{DynamicResource FontAweSomeSkin_White}"
Text="{Binding ImagePath, Converter={StaticResource ConvertFontIconEncode}}"
FontSize="{DynamicResource TreeMainItemLabelFontSize}" VerticalAlignment="Center" Opacity="0.7"/>
</Grid>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource Timeline1}"/>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard x:Name="Timeline2_BeginStoryboard" Storyboard="{StaticResource Timeline2}"/>
</Trigger.ExitActions>

<Setter TargetName="Rectangle1" Property="Fill" Value="#303030"/>
<Setter TargetName="txtIco" Property="Opacity" Value="1"/>
<Setter TargetName="_Border" Property="Background" Value="#303030"/>
<Setter TargetName="pop" Property="IsOpen" Value="true"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter TargetName="pop" Property="IsOpen" Value="false"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="_Border" Property="Background" Value="#303030"/>
<Setter TargetName="pop" Property="IsOpen" Value="false"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

猜你喜欢

转载自www.cnblogs.com/Li-JiaWen/p/11317299.html