none
WPF中 用switch实现Menu事件调用 RRS feed

  • 问题

  • xaml 代码

    Control x:Class="Luobenshidai.administrator.MainWindowMenuUserControl"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 mc:Ignorable="d"
                 d:DesignHeight="28" d:DesignWidth="1345" xmlns:navigation="http://schemas.actiprosoftware.com/winfx/xaml/navigation">

        <UserControl.Resources>
            <Style TargetType="{x:Type Image}">
                <Setter Property="Width" Value="36" />
                <Setter Property="Height" Value="28" />
            </Style>
            <Style TargetType="{x:Type Menu}">
                <Setter Property="FontSize" Value="20" />
            </Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="HorizontalAlignment" Value="Right"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="Width" Value="30"/>
                <Setter Property="Height" Value="28"/>
            </Style>
        </UserControl.Resources>

            <Grid Height="28" HorizontalAlignment="Left" VerticalAlignment="Top"   Width="1345" >
            <Grid.RowDefinitions>
                <RowDefinition Height="28" MinHeight="28" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition  Width="1345" />
            </Grid.ColumnDefinitions>
            <DockPanel Grid.Row="0" Grid.Column="0" Margin="0" HorizontalAlignment="Left" VerticalAlignment="Center">
                <Menu >
                    <MenuItem  Header="山之子" Padding="0" >
                         <MenuItem.Icon>
                            <Image  Source="/Luobenshidai;component/ico/Personal%20photographs/李明春.jpg" />
                        </MenuItem.Icon>
                        <MenuItem Header="私人空间" Name="Sirenkongjian"  Click="MainMenuChoose_Loaded">
                            <MenuItem.Icon>
                                <Image  Source="/Luobenshidai;component/ico/gift%20Icons/1027.gif" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="生活杂记" Name="Shenhuozaji"  Click="MainMenuChoose_Loaded">
                            <MenuItem.Icon>
                                <Image   Source="/Luobenshidai;component/ico/Amora%20Icons/Text%20Editor.png" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="俗人见解" Name="Surenjianjie"  Click="MainMenuChoose_Loaded">
                            <MenuItem.Icon>
                                <Image  Source="/Luobenshidai;component/ico/WinTypeIcon%20Quality%20Icons/Emule.Folder.png" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="电 话 本" Name="Dianhuaben"  Click="MainMenuChoose_Loaded">
                            <MenuItem.Icon>
                                <Image   Source="/Luobenshidai;component/ico/Angular%20Stock%20Icons/contacts.png" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="收    藏" Name="Shoucang"  Click="MainMenuChoose_Loaded">
                            <MenuItem.Icon>
                                <Image   Source="/Luobenshidai;component/ico/Angular%20Stock%20Icons/import2.png" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="个人爱好" Name="Gerenaihai"  Click="MainMenuChoose_Loaded">
                            <MenuItem.Icon>
                                <Image Source="/Luobenshidai;component/ico/Alien%20Aqua%20Icons/AlienAqua%20USB.png" />
                            </MenuItem.Icon>
                        </MenuItem>
                    </MenuItem>
                </Menu>
                <Menu >
                    <MenuItem  Header="工 作" Padding="0" >
                        <MenuItem.Icon>
                            <Image Source="/Luobenshidai;component/ico/vehicles%20Icons/11.gif" />
                        </MenuItem.Icon>
                        <MenuItem Header="日程安排" Name="Richenanpai"  Click="MainMenuChoose_Loaded">
                            <MenuItem.Icon>
                                <Image  Source="/Luobenshidai;component/ico/vehicles%20Icons/46.gif" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="信息查询" Name="Xingxichaxun"  Click="MainMenuChoose_Loaded">
                            <MenuItem.Icon>
                                <Image  Source="/Luobenshidai;component/ico/Solid%20XP%20Stock%20Icons/print-preview.png" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="记 事 本" Name="Jishibeng"  Click="MainMenuChoose_Loaded">
                            <MenuItem.Icon>
                                <Image   Source="/Luobenshidai;component/ico/Blue%20Sky%20Multimedia%20Icons/album_256x256.tif" />
                            </MenuItem.Icon>
                        </MenuItem>
                    </MenuItem>
                </Menu>
            </DockPanel>
            
    </Grid>
    </UserControl>

    后台代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    using Luobenshidai;
    using Luobenshidai.shanzhizi;
    using Luobenshidai.work;

    namespace Luobenshidai.administrator
    {
            public partial class MainWindowMenuUserControl : UserControl
        {
            public MainWindowMenuUserControl()
            {
                InitializeComponent();
            }

           

           MainWindowMenuUserControl MWMUC = new MainWindowMenuUserControl();
            Menu menu = new Menu();
            MenuItem menuitem = new MenuItem();
            Application application = new Application();

            String menuitemname;
            public String Menuitemname
            {
                get { return menuitemname; }
                set { menuitemname = menuitem.Name; }
            }

            private void MainMenuChoose_Loaded(object sender, RoutedEventArgs e)
            {

     

                switch (menuitemname != null)

              {
                 case Sirenkongjian:
                    application.Run(new Sirenkongjian());
                    break;

                case Shenhuozaji:
                    application.Run(new Shenhuozaji());
                    break;
                case Surenjianjie:
                    application.Run(new Surenjianjie());
                    break;
                case Dianhuaben:
                   application.Run(new Dianhuaben());
                    break;
                case Shoucang:
                    application.Run(new Shoucang());
                    break;
                case Richenanpai:
                    application.Run(new Richenanpai());
                    break;
                case Xingxichaxun:
                    application.Run(new Xingxichaxun());
                    break;
                case Jishibeng:
                    application.Run(new Jishibeng());
                    break;
                case Gerenaihai:
                    application.Run(new Gerenaihai());
                    break;
             }
            }

        }
    }

    我编写了Menu控件前台代码,经过测试没有问题,并在MenuIten中绑定了MainMenuChoose_Loaded事件控件,并在后台代码中编写了MainMenuChoose_Loaded事件控件代码,其主要功能是通过前台MenuIten点击调用MainMenuChoose_Loaded事件,均判断实现第二窗口调用(二窗口调用通过application.Run(new Gerenaihai());实现,其代码与省去),在编译中无法实现编译(加黑代码),提示“无法将代码System.Windows.Controls.MenuItem转化为 bool” 请问我要如何编写才能实现该功能

    注:加黑部分代码如何编写才能实现该功能

    2012年3月29日 13:20

答案

全部回复

  • 把你的代码给你捎加解释:

    我能够理解你要做的东西,但是我不得不否定你的做法,是错误的,这里有很多问题,包括UserControl你无法将这么多MenuItem的名字设置到一个menuitemname属性上;你不应该调用application.Run(),除非这个Run是你自己的方法,不是说起启动一个应用程序。

    下面是根据你的代码修改的,请参考:

        private void MainMenuChoose_Loaded(object sender, RoutedEventArgs e)
        {
          MenuItem item = sender as MenuItem;
          if (item != null)
          {
            switch (item.Name)
            {
              case "Sirenkongjian":
                MessageBox.Show("Sirenkongjian");
                break;
              case "Shenhuozaji":
                MessageBox.Show("Shenhuozaji");
                break;
              case "Surenjianjie":
                MessageBox.Show("Surenjianjie");
                break;
              case "Dianhuaben":
                MessageBox.Show("Dianhuaben");
                break;
              case "Shoucang":
                MessageBox.Show("Shoucang");
                break;
              case "Richenanpai":
                MessageBox.Show("Richenanpai");
                break;
              case "Xingxichaxun":
                MessageBox.Show("Xingxichaxun");
                break;
              case "Jishibeng":
                MessageBox.Show("Jishibeng");
                break;
              case "Gerenaihai":
                MessageBox.Show("Gerenaihai");
                break;
            }
          }
        }

    Switch不支持匹配一个对象类型的,所以我们要么去匹配这个MenuItem的Name,要么用一些Int/Long的整型数值去匹配。

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年3月30日 7:41
    版主
  • ?????

    2012年3月31日 6:54
  • 很感谢你的答复,一看就很专业,个人水平原因,部分代码看不懂,如有注解就好了,正在努力研究。

    这有一个问题贴出来想起帮忙。

     

    原因:我设计了一个WPF项目,在项目中有一个MainWindow.xaml主页,并设计了一个自定义用户控件WindowMainControl.xaml,控件中定义了一个DockPanel工具条,主要目的是通过鼠标在MainWindow.xaml主窗口距顶部0~10的位置获得焦点,调用自定义用户控件(显示用户控件),当鼠标离开距主窗口顶部0~10的位置失去焦点,用户控件自动隐藏。

    问题:我在MainWindow.xaml主页中引用了自定义用户控件,并设计设用户控件的现实与隐藏,但调试效果显示用户控件直接显示,不能实现隐藏功能。

    1.程序设计结构如下图:  

    2.自定义用户控件WindowMainControl.xaml,控件中定义了一个DockPanel代码,设计希望将通过焦点在主页中显示DockPanel控件,自定义用户控件WindowMainControl.xaml代码下:

    <UserControl x:Class="Shanzhizi.Control.WindowMainControl.WindowMainControl"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 mc:Ignorable="d" d:DesignHeight="35" d:DesignWidth="1354">
        <UserControl.Resources>
            <ResourceDictionary Source="WindowMainControlDictionary.xaml" />
        </UserControl.Resources>
        <Grid >
            <DockPanel Name="MenuDocKpanel"  Style="{StaticResource DockPanelResource}">
                <StackPanel Style="{StaticResource StackPanelLeftResource}" >
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/home.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/stuff.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/TN_psd.JPG" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/album.tif" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/RTF.File.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/PDF.File.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/IExplorer.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/mWorld_green.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/Musica_min.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/mWorld_cd_Yellow.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/applications.png" />
                    </Button>
                </StackPanel>
                <StackPanel Style="{StaticResource StackPanelRightResource}" >
                    <Button Click="MinWindow">
                        <Image Source="/Shanzhizi;component/Control/ico/-.png" />
                    </Button>
                    <Button Click="FullWindow">
                        <Image Source="/Shanzhizi;component/Control/ico/+.png" />
                    </Button>
                    <Button Click="CloseWindow">
                        <Image Source="/Shanzhizi;component/Control/ico/delete.png" />
                    </Button>
                </StackPanel>
            </DockPanel>
        </Grid>
    </UserControl>

    用户控件后台代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    namespace Shanzhizi.Control.WindowMainControl
    {
        /// <summary>
        /// WindowMainControl.xaml 的交互逻辑
        /// </summary>
        public partial class WindowMainControl : UserControl
        {
            public WindowMainControl()
            {
                InitializeComponent();
            }

            private void CloseWindow(object sender, RoutedEventArgs e)
            {
                Application.Current.MainWindow.Close();
            }

            private void FullWindow(object sender, RoutedEventArgs e)
            {
                Application.Current.MainWindow.WindowState= WindowState.Maximized;
            }

            private void MinWindow(object sender, RoutedEventArgs e)
            {
                Application.Current.MainWindow.WindowState = WindowState.Minimized;
            }

        }
    }

    注:后台代码没有问题,在主页中引用也无问题

    3.主页MainWindow.xaml代码如下:

    <Window x:Class="Shanzhizi.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:windowmaincontrol="clr-namespace:Shanzhizi.Control.WindowMainControl"
            Title="私人空间"    Width="1360" Height="740" 
            WindowStartupLocation="CenterScreen"  WindowStyle="None"
            PreviewMouseMove="Window_PreviewMouseMove">

        <Grid >
            <windowmaincontrol:WindowMainControl
                LostFocus="WindowMainControl_LostFocus"
                GotFocus="WindowMainControl_GotFocus"
                ContextMenuOpening="WindowMainControl_ContextMenuOpening"/>
        </Grid>
       
    </Window>

    主页后台代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;


    //引入控件
    using Shanzhizi.Control.WindowMainControl;

    namespace Shanzhizi
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }

            //自定义用户控件声明
            WindowMainControl WMC = new WindowMainControl();

            private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
            {
                // 通过鼠标焦点获自定义用户控件自动显示与隐藏
                //假设标题栏在0和10之间
                if (e.GetPosition(this).Y >= 0 && e.GetPosition(this).Y < 10)
                {
                    if (WMC.Visibility != Visibility.Visible)
                    {
                        WMC.Visibility = Visibility.Visible;
                    }
                }
            }
            //获得焦点
            private void WindowMainControl_LostFocus(object sender, RoutedEventArgs e)
            {
                WMC.Visibility = Visibility.Collapsed;
            }

            //失去焦点
            private void WindowMainControl_GotFocus(object sender, RoutedEventArgs e)
            {
                WMC.Visibility = Visibility.Visible;
            }
            //窗体事件焦点
            private void WindowMainControl_ContextMenuOpening(object sender, ContextMenuEventArgs e)
            {
                WMC.Visibility = Visibility.Visible;
            }
        }
    }

    调试效果:能正常通过编译,并正常调试;

    本人设计问题考虑:通过鼠标MouseMove获得焦点(焦点取在主页顶部0~10之间)显示自定义用户控件,鼠标离开焦点隐藏用户控件;

    问题分析:

    1.自定义用户控件引入方法错误;

    2.

                PreviewMouseMove="Window_PreviewMouseMove">

                LostFocus="WindowMainControl_LostFocus"
                GotFocus="WindowMainControl_GotFocus"
                ContextMenuOpening="WindowMainControl_ContextMenuOpening"

    鼠标事件应用错误;

    3. private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
            {
                // 通过鼠标焦点获自定义用户控件自动显示与隐藏
                //假设标题栏在0和10之间
                if (e.GetPosition(this).Y >= 0 && e.GetPosition(this).Y < 10)
                {
                    if (WMC.Visibility != Visibility.Visible)
                    {
                        WMC.Visibility = Visibility.Visible;
                    }
                }
            }

    未获取焦点、焦点设置错误。

     

    2012年5月18日 2:41
  • 感谢回答,正在调试中。
    2012年5月18日 2:43