關於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 的位置所需垂直移動多少個象素

    <Style TargetType="Button" x:Key="btnButtonPopupStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="Item" BorderThickness="1" Cursor="Hand" Background="{DynamicResource Background-Info1}" CornerRadius="0,0,0,0">
                        <DockPanel VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5">
                            <Viewport3D Grid.Column="0" Width="30" Height="30" Margin="0,0,0,0">
                                <Viewport3D.Camera>
                                    <PerspectiveCamera Position="0 0 700" LookDirection="0 0 -1" />
                                </Viewport3D.Camera>
                                <Viewport3D.Children>
                                    <ContainerUIElement3D>
                                        <Viewport2DVisual3D>
                                            <Viewport2DVisual3D.Geometry>
                                                <MeshGeometry3D Positions="-200 200 0  -200 -200 0  200 -200 0  200 200 0" TriangleIndices="0 1 2  0 2 3" TextureCoordinates="0 0  0 1  1 1  1 0"/>
                                            </Viewport2DVisual3D.Geometry>
                                            <Viewport2DVisual3D.Material>
                                                <DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True"/>
                                            </Viewport2DVisual3D.Material>
                                            <Viewport2DVisual3D.Visual>
                                                <Border Grid.Column="1" >
                                                    <TextBlock Style="{StaticResource HSFontAweSome}" Foreground="#fff" Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Content}" Margin="10" VerticalAlignment="Center"></TextBlock>
                                                </Border>
                                            </Viewport2DVisual3D.Visual>
                                        </Viewport2DVisual3D>
                                        <Viewport2DVisual3D>
                                            <Viewport2DVisual3D.Geometry>
                                                <MeshGeometry3D Positions="200 200 0  200 -200 0  -200 -200 0  -200 200 0" TriangleIndices="0 1 2  0 2 3" TextureCoordinates="0 0  0 1  1 1  1 0"/>
                                            </Viewport2DVisual3D.Geometry>
                                            <Viewport2DVisual3D.Material>
                                                <DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True"/>
                                            </Viewport2DVisual3D.Material>
                                            <Viewport2DVisual3D.Visual>
                                                <Border Grid.Column="1">
                                                    <TextBlock Style="{StaticResource HSFontAweSome}" Foreground="#fff" Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Content}" Margin="10" VerticalAlignment="Center"></TextBlock>
                                                </Border>
                                            </Viewport2DVisual3D.Visual>
                                        </Viewport2DVisual3D>
                                        <ContainerUIElement3D.Transform>
                                            <RotateTransform3D>
                                                <RotateTransform3D.Rotation>
                                                    <AxisAngleRotation3D x:Name="Rotation3D" Angle="0" Axis="0 1 0"/>
                                                </RotateTransform3D.Rotation>
                                            </RotateTransform3D>
                                        </ContainerUIElement3D.Transform>
                                    </ContainerUIElement3D>
                                    <ModelVisual3D>
                                        <ModelVisual3D.Content>
                                            <DirectionalLight Color="Transparent"/>
                                        </ModelVisual3D.Content>
                                    </ModelVisual3D>
                                </Viewport3D.Children>
                            </Viewport3D>
                            <TextBlock x:Name="ItemLabel" Foreground="#fff" Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Tag}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,10,0"/>
                            <TextBlock Style="{StaticResource HSFontAweSome}" Margin="0,-5,0,0" Foreground="#fff" Text="&#xf0dd;" FontSize="{DynamicResource DefaultFontSize}"></TextBlock>
                            <Popup x:Name="popShow"
                               IsOpen="False" StaysOpen="False"
                               PopupAnimation="Slide" AllowsTransparency="True"
                               PlacementTarget="{Binding ElementName= Item}"
                               Placement="Bottom">
                                <StackPanel>
                                    <Button Content="&#xf08e;" Tag="導入Execl" Style="{DynamicResource btnButtonStyle_Export}" Command="{Binding SimpleInteractionCommand}" CommandParameter="Execl"></Button>
                                    <Button Content="&#xf045;" Tag="導出Xml" Style="{DynamicResource btnButtonStyle_Export}"  Command="{Binding SimpleInteractionCommand}" CommandParameter="Xml"></Button>
                                </StackPanel>
                            </Popup>
                        </DockPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="Item" Property="Background" Value="#00a3d2" />
                            <Setter TargetName="ItemLabel" Property="Foreground" Value="#fff" />
                            <Setter TargetName="popShow" Property="IsOpen" Value="true"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="false">
                            <Setter TargetName="popShow" Property="IsOpen" Value="false"/>
                        </Trigger>
                        <EventTrigger RoutedEvent="Mouse.MouseEnter">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="Rotation3D" Storyboard.TargetProperty="Angle" To="180" Duration="0:0:0.3"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="Mouse.MouseLeave">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="Rotation3D" Storyboard.TargetProperty="Angle" To="0" Duration="0:0:0.3"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM