WPF功能菜單懸浮顯示


需求:當鼠標移入某個菜單功能,浮現其子功能項,以便用戶進行下一步操作。

效果圖:

由於該浮窗的內容較多,單獨做成一個資源樣式進行引用。 

使用WPF資源樣式的方法參考這篇文章:講解的特別詳細

https://www.cnblogs.com/zhili/p/WPFResourceAndStyle.html

前台布局:

<!--懸浮菜單-->
        <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>
View Code

說明:這里用的是ItemsControl控件,是加載功能菜單項,在通過DataTemplate去制定其需要的外表。

按鈕樣式:

<!--懸浮菜單按鈕-->
    <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>
View Code

說明:這里使用了WPF-關鍵幀動畫,簡單將懸浮效果做成鼠標移入移出呈波浪狀的效果,Popup包含ItemsControl控件用於加載功能菜單子項


免責聲明!

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



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