silverlight中添加给TextBox批量添加右键菜单

基于此人写的代码修改优化后支持批量给所有TextBox添加右键菜单,而菜单共用一个,节省资源。

    public static class CreatMenuForTextBox
    {
        // 菜单  
        static ContextMenu menu;
        //TextBox  
        static TextBox textBox;

        /// <summary>  
        /// 为任意元素的内部TextBox添加右键菜单
        /// </summary>
        public static void creatMenu(DependencyObject obj)
        {
            menu = new ContextMenu();
            menu.Items.Add(getItem("剪切","../Images/1.jpg"));
            menu.Items.Add(getItem("复制",null));
            menu.Items.Add(getItem("粘贴",null));
            menu.Items.Add(new Separator());
            menu.Items.Add(getItem("选择全部",null));
            menu.Opened += menu_Opened;
            SetMenu(obj);
        }

        static void SetMenu(DependencyObject obj)
        {
            var count = VisualTreeHelper.GetChildrenCount(obj);
            for (int i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(obj,i);
                if (child is TextBox)
                {
                    ((TextBox)child).MouseRightButtonDown += TextBox_MouseRightButtonDown;
                }
                else
                {
                    SetMenu(child);
                }
            }
        }

        static void TextBox_MouseRightButtonDown(object sender,MouseButtonEventArgs e)
        {
            e.Handled = true;
            textBox = sender as TextBox;
            textBox.Focus();
            menu.IsOpen = true;
        }

        static void menu_Opened(object sender,RoutedEventArgs e)
        {
            textBox.Focus();
        }

        static MenuItem getItem(string header,string imagePath)
        {
            MenuItem item = new MenuItem();
            item.Header = header;
            if (imagePath != null)
            {
                Image im = new Image();
                im.source = new ImageSourceConverter().ConvertFromString(imagePath) as ImageSource; 
                item.Icon = im;
            }
            item.Click += new RoutedEventHandler(item_Click);
            return item;
        }

        static void item_Click(object sender,RoutedEventArgs e)
        {
            MenuItem menuItem = (MenuItem)sender;
            try
            {
                execute(menuItem.Header.ToString());
            }
            catch (Exception)
            {
            }
            textBox.Focus();
        }

        static void execute(string selected)
        {
            switch (selected)
            {
                case "剪切":
                    Clipboard.SetText(textBox.SelectedText);
                    textBox.SelectedText = "";
                    textBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                    break;
                case "复制":
                    Clipboard.SetText(textBox.SelectedText);
                    break;
                case "粘贴":
                    textBox.SelectedText = Clipboard.GetText();
                    textBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                    break;
                case "选择全部":
                    textBox.SelectAll();
                    break;
                default:
                    break;
            }
        }
    }
2013-11-20修改添加了剪切和粘贴动作对绑定数据源的更新。

相关文章

如何在Silverlight4(XAML)中绑定IsEnabled属性?我试过简单的...
我正在编写我的第一个vb.net应用程序(但我也会在这里标记c#,...
ProcessFile()是在UIThread上运行还是在单独的线程上运行.如...
我从同行那里听说,对sharepoint的了解对职业生涯有益.我们不...
我正在尝试保存一个类我的类对象的集合.我收到一个错误说明:...
我需要根据Silverlight中的某些配置值设置给定控件的Style.我...