WPF 制作下拉式菜单栏

开发工具与关键技术:Microsoft Visual Studio 2017、WPF

这里这个下拉式菜单栏用的是一个RadioButton(单选按钮)Popup(包含内容的弹出项)组合形成的下拉式菜单栏。
菜单栏显示:点击
RadioButton
即可显示。
菜单栏隐藏:点击菜单栏外 屏幕任意一处 即可隐藏或选中该菜单栏内的某一项时隐藏菜单栏,但是选中该菜单栏内的某一项时隐藏菜单栏这个代码我这里因为菜单栏的每一项的点击事件里面含有其他代码所以不方便就没有给出来,需要的话自行加上,如果你是按照我下面写的方法来做的话,那么你只需要在菜单栏的每一项的点击事件里面设置RadioButton的IsChecked属性值等于false即可,因为我把这个属性值跟Popup的IsOpen的值绑定在一起了。

菜单栏显示效果图:

在这里插入图片描述
xaml 代码:

<!--设置-->
<RadioButton Content="设置" Width="100" Height="25" FontSize="15" Style="{StaticResource SetRadioButtonStyle}" x:Name="butSetText" HorizontalAlignment="Left" Click="ButSetText_Click"/>

<!--设置 明细菜单-->
<Popup StaysOpen="False" x:Name="setTextelPopup" Grid.Column="2" Grid.RowSpan="2" AllowsTransparency="true" Height="Auto" Width="{Binding ActualWidth,ElementName=butSetText}" Placement="Bottom" PopupAnimation="Slide" PlacementTarget="{Binding ElementName=butSetText}" IsOpen="{Binding  ElementName=butSetText,Path=IsChecked,  Mode=OneWay}"  LostFocus="SetTextelPopup_LostFocus" Closed="SetTextelPopup_Closed" Opened="SetTextelPopup_Opened">
    <Border BorderBrush="#959593" BorderThickness="0.5" x:Name="setTextelBorder">
        <StackPanel Width="100"  Height="Auto" HorizontalAlignment="Right" Panel.ZIndex="1" VerticalAlignment="Top" Background="White" Orientation="Vertical">
            <Button Content="软件版本" Style="{StaticResource SetTextelStyle}" x:Name="BtnSoftwareVersion" />
            <Button Content="日志记录" Style="{StaticResource SetTextelStyle}" x:Name="BtnLogRecord" />
            <Button Content="系统配置" Style="{StaticResource SetTextelStyle}" x:Name="BtnSystemConfiguration"/>
        </StackPanel>
    </Border>
</Popup>

RadioButton 样式代码:

    <!--#region 主页面 "设置"按钮样式-->
    <Style TargetType="RadioButton" x:Key="SetRadioButtonStyle">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="FontFamily" Value="{StaticResource YaHeiLight}"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Margin" Value="0,3,0,0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="RadioButton">
                    <Border x:Name="border" BorderBrush="Transparent" BorderThickness="0" Background="Transparent">
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                            <Image x:Name="image" Source="/Images/MainImages/Setlogo.png" Stretch="Fill" Width="15" Height="15" Margin="5,0,2,0"/>
                            <TextBlock Text="{TemplateBinding Content}" x:Name="textBlock" Foreground="{TemplateBinding Foreground}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            <Image x:Name="arrow" Source="/Images/Logo/ComboBoxOpenLogo.png" Stretch="Fill" Width="21" Height="21" Margin="2,0,2,0"/>
                        </StackPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="CornflowerBlue" TargetName="textBlock"/>
                            <Setter Property="Source" Value="/Images/MainImages/MouseOverSetlogo.png" TargetName="image"/>
                            <Setter Property="Source" TargetName="arrow" Value="/Images/Logo/MouseOverComboBoxOpenLogo.png"/>
                        </Trigger>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="RenderTransform" TargetName="arrow">
                                <Setter.Value>
                                    <TransformGroup>
                                        <RotateTransform Angle="-180" CenterY="10" CenterX="10.5"/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsEnabled" Value="False"/>
                            </MultiTrigger.Conditions>
                            <MultiTrigger.Setters>
                                <Setter Property="Background" Value="#FFFFFF" TargetName="border"/>
                            </MultiTrigger.Setters>
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <!--#endregion-->


xaml.cs 代码:

        /// <summary>
        /// 记录IsChecked上次的值,默认为False
        /// </summary>
        bool rememberChecked = false;

        /// <summary>
        /// “设置”按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButSetText_Click(object sender, RoutedEventArgs e)
        {
            if (rememberChecked)
            {
                butSetText.IsChecked = false;
                rememberChecked = false;
            }
            else
            {
                butSetText.IsChecked = true;
                rememberChecked = true;
            }
        }
        /// <summary>
        /// "设置明细"失去焦点时发生事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SetTextelPopup_LostFocus(object sender, RoutedEventArgs e)
        {
            butSetText.IsChecked = false;
            butSetText.IsEnabled = true;
            rememberChecked = false;
        }
        /// <summary>
        /// "设置明细"隐藏时发生事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SetTextelPopup_Closed(object sender, EventArgs e)
        {
            butSetText.IsChecked = false;
            butSetText.IsEnabled = true;
            rememberChecked = false;
        }
        /// <summary>
        /// "设置明细"显示时发生事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SetTextelPopup_Opened(object sender, EventArgs e)
        {
            butSetText.IsEnabled = false;
        }
        

说明:后面3个事件在xaml前台的Popup控件里面都是可以直接定义创建的,如图:

在这里插入图片描述

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
WPF实现侧拉菜单栏可以通过以下步骤实现: 1.创建一个具有侧拉菜单栏的主窗口 2.在主窗口中添加一个侧拉菜单栏控件,并设置其大小和位置 3.在侧拉菜单栏控件中添加需要显示的菜单项,并设置其大小、位置和相关事件处理程序 4.在主窗口中添加一个主内容区域控件,并设置其大小和位置 5.在主内容区域控件中添加需要显示的内容,并设置其大小、位置和相关事件处理程序 代码示例: ``` <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Border Grid.Column="0" Background="Gray" Width="150" VerticalAlignment="Stretch"> <StackPanel> <Button Content="Menu Item 1" Margin="10"/> <Button Content="Menu Item 2" Margin="10"/> <Button Content="Menu Item 3" Margin="10"/> </StackPanel> </Border> <Border Grid.Column="1" Background="White" Margin="10"> <TextBlock Text="Main Content Area" Margin="10"/> </Border> </Grid> </Window> ``` 在上面的示例中,我们使用Grid布局来实现侧拉菜单栏和主内容区域的布局,同时使用Border控件来包裹菜单项和主内容。在侧拉菜单栏中,我们使用StackPanel来布局菜单项,并在每个菜单项上添加了Margin属性来设置间距。在主内容区域中,我们使用TextBlock控件来显示内容。 需要注意的是,上述示例仅供参考,您可以根据自己的实际需要进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值