基本信息
源码名称:WPF TextBox一个缺陷的修改
源码大小:0.31M
文件格式:.rar
开发语言:C#
更新时间:2020-05-12
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 1 元 
   源码介绍

WPF TextBox如果行数过多,比如1、2千行,这时候按下ctrl-a进行全选(卡顿很慢),再点击右键,这时候右键菜单要么打开迟缓,要么不停闪烁,点击菜单项后,不会执行相应的菜单命令。

根据这个情况,重建一个菜单,虽然ctrl-a进行全选还很慢,但菜单显示及执行功能不受影响了。组件要添加到根部的canvas里面。

在vs2017编译后不出现异常,vs2019有几率出现粘贴板错误,自行百度修正。



using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;

public class TextBoxHelper
{
    public Color BackgroundColor = Colors.White; // 背景色
    public Color TextColor = Colors.Black; // 文字颜色
    public Color LinecodeBackground = Colors.Gainsboro; // 显示行号背景色
    public Color LinecodeColor = Colors.SteelBlue; // 显示行号文字色
    public FontFamily  fontFamily =  new FontFamily("Microsoft YaHei");  // 字体
    public double Fontsize = 12; // 字体大小
    public double Line_Height = 20; // 行高度
    public double Linecode_Width = 35; // 行号宽度
    public double Linecode_Interval = 5; // 行号右侧间隔
    public TextBox Editor;  // TextBox组件
    public EditMenu editMenu = new EditMenu();

    private ScrollTemplate scrollTemplate_TextBox;
    private Canvas Editor_Box;
    private Canvas Linenumber;
    private double per_height = 0;
    private Rect control_rect;

    /// <summary>
    /// 带行号的TextBox
    /// </summary>
    /// <param name="obj">父容器</param>
    /// <param name="x">位置 x</param>
    /// <param name="y">位置 y</param>
    /// <param name="width">宽度</param>
    /// <param name="height">高度</param>
    public void LinenumberEditor(Canvas obj, double x, double y, double width, double height)
    {
        editMenu = new EditMenu()
        {
            Visibility = Visibility.Hidden
        };
        Panel.SetZIndex(editMenu,999);
        obj.Children.Add(editMenu);
        per_height = height;
        
        Editor_Box = new Canvas()
        {
            Width = width  Linecode_Width,
            Height = height,
            Background = new SolidColorBrush(BackgroundColor),
            Clip = new RectangleGeometry(new Rect(0, 0, width, height))
        };
        Editor_Box.PreviewMouseDown  = Editor_PreviewMouseDown;
        Canvas.SetLeft(Editor_Box, x);
        Canvas.SetTop(Editor_Box, y);

        Editor = new TextBox()
        {
            BorderThickness = new Thickness(0),
            ContextMenu = null,
            AcceptsReturn = true,
            FontFamily = fontFamily,
            FontSize = Fontsize,
            AcceptsTab = true,
            Foreground = new SolidColorBrush(TextColor),
            Background =Brushes.Transparent,
        };
        Editor.PreviewKeyDown  = Editor_PreviewKeyDown;
        Editor.TextChanged  = Editor_TextChanged;
        Editor.SelectionChanged  = Editor_SelectionChanged;
        Editor.LostFocus  = Editor_LostFocus;
        TextBlock.SetLineHeight(Editor, Line_Height);
        Canvas.SetLeft(Editor, 30);

        WrapPanel text_panel = new WrapPanel()
        {
            Orientation = Orientation.Vertical,
        };
        text_panel.Children.Add(Editor);
        
        scrollTemplate_TextBox = new ScrollTemplate();
        scrollTemplate_TextBox.ScrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
        scrollTemplate_TextBox.ScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
        scrollTemplate_TextBox.ScrollViewer.Background = Brushes.WhiteSmoke;
        scrollTemplate_TextBox.InitializeScrollViewer(Editor_Box, text_panel, Linecode_Width, 0, width- Linecode_Width, height, ScrollViewer_Type.Effect);
        scrollTemplate_TextBox.ScrollViewer.ScrollChanged  = (s, e) => {
            GeneralTransform generalTransform = Editor.TransformToAncestor(Editor_Box);
            Point point = generalTransform.Transform(new Point(0, 0));
            Canvas.SetTop(Linenumber, point.Y);
        };
        scrollTemplate_TextBox.ScrollViewer.PreviewMouseDown  = (s, e) => {
             obj.Dispatcher.BeginInvoke(DispatcherPriority.Background,
            (Action)(() => { Keyboard.Focus(Editor); }));
        };

        Linenumber = new Canvas()
        {
            Width = Linecode_Width,
            Height = height,
            Background = new SolidColorBrush(LinecodeBackground)
        };
        Editor_Box.Children.Add(Linenumber);

        TextBlock Linecode = new TextBlock()
        {
            Height = Line_Height,
            Width = Linecode_Width - Linecode_Interval,
            TextAlignment = TextAlignment.Right,
            Text = "1",
            FontFamily = fontFamily,
            FontSize = Fontsize,
            Foreground = new SolidColorBrush(LinecodeColor),
            Background = Brushes.Transparent
        };
        Linenumber.Children.Add(Linecode);

        Editor_Box.MouseDown  = (s, e) => { Editor.Focus(); };
        obj.Children.Add(Editor_Box);

        (obj as FrameworkElement).PreviewMouseDown  = TextBoxHelper_PreviewMouseDown;
        editMenu.IsVisibleChanged  = EditMenu_IsVisibleChanged;

        control_rect = new Rect(Canvas.GetLeft(Editor_Box), Canvas.GetTop(Editor_Box), Editor_Box.Width - Linecode_Width, Editor_Box.Height);
    }

    private void Editor_LostFocus(object sender, RoutedEventArgs e)
    {
        Set_TextBoxDefrag();
    }

    private void TextBoxHelper_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        FrameworkElement pL = (FrameworkElement)sender;
        Point point = Mouse.GetPosition(pL);

        if (!control_rect.Contains(point))
        {
            if (editMenu.ShowMenu)
            {
                editMenu.Visibility = Visibility.Hidden;
                editMenu.ShowMenu = false;
            }
            Editor.Select(Editor.SelectionStart, 0);
        }
    }

    private void EditMenu_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        if (editMenu.IsVisible)
        {
            editMenu.Option = -1;
            Set_Menu();
        }
        else
        {
            if (editMenu.Option==0) // cut
            {
                Clipboard.Clear();
                Clipboard.SetDataObject(Editor.SelectedText);
                Editor.SelectedText = "";
            }
            else
            if (editMenu.Option == 1) // copy
            {
                Clipboard.Clear();
                Clipboard.SetDataObject(Editor.SelectedText);
            }
            else
            if (editMenu.Option == 2) // past
            {
                if (Editor.SelectionLength > 0)
                {
                    Editor.SelectedText = Clipboard.GetText();
                }
                else
                {
                    Editor.Select(Editor.SelectionStart, 0);
                    Editor.SelectedText = Clipboard.GetText();
                }
            }
            else
            if (editMenu.Option == 3) // select all
            {
                Editor.SelectAll();
            }
            else
            if (editMenu.Option == 4) // clear
            {
                Editor.SelectedText = "";
            }
        }
    }

    private void Editor_SelectionChanged(object sender, RoutedEventArgs e)
    {
        Set_Menu();
    }

    private void Editor_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            if (editMenu.ShowMenu)
            {
                editMenu.Visibility = Visibility.Hidden;
                editMenu.ShowMenu = false;
            }
            Editor.Select(Editor.SelectionStart, 0);
        }
        else
        if (e.RightButton == MouseButtonState.Pressed)
        {
            Set_Menu();
            Point point = Mouse.GetPosition(Editor_Box);
            double x = point.X   210 > Editor_Box.Width ? (point.X - 150) : point.X 25; 
            double y = point.Y   120 > Editor_Box.Height ? (point.Y - 110) : point.Y   20;
            if (!editMenu.ShowMenu)
            {
                Canvas.SetLeft(editMenu,x);
                Canvas.SetTop(editMenu,y);
                editMenu.Visibility = Visibility.Visible;
                editMenu.ShowMenu = true;
            }
            else
            {
                Canvas.SetLeft(editMenu, x);
                Canvas.SetTop(editMenu, y);
            }
        }
    }

    private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        MenuItem pL = (MenuItem)sender;
        if ((int)pL.Tag == 0)
        {
            Clipboard.Clear();
            Clipboard.SetText(Editor.SelectedText);
            Editor.SelectedText = "";
        }
        else
        if ((int)pL.Tag == 1)
        {
            Clipboard.SetText(Editor.SelectedText);
        }
        if ((int)pL.Tag == 2)
        {
            if (Editor.SelectionLength > 0)
            {
                Editor.SelectedText = Clipboard.GetText();
            }
            else
            {
                Editor.Select(Editor.SelectionStart, 0);
                Editor.SelectedText = Clipboard.GetText();
            }
        }
        else
        if ((int)pL.Tag == 3)
        {
            Editor.SelectAll();
        }
        else
        if ((int)pL.Tag == 4)
        {
            Editor.SelectedText = "";
        }
    }

    private void Editor_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (editMenu.ShowMenu)
        {
            editMenu.Visibility = Visibility.Hidden;
            editMenu.ShowMenu = false;
        }

        if (e.Key==Key.PageDown)
        {
            e.Handled = true;
            scrollTemplate_TextBox.ScrollViewer.ScrollToVerticalOffset(scrollTemplate_TextBox.ScrollViewer.VerticalOffset   per_height);
        }
        else
        if (e.Key == Key.PageUp)
        {
            e.Handled = true;
            scrollTemplate_TextBox.ScrollViewer.ScrollToVerticalOffset(scrollTemplate_TextBox.ScrollViewer.VerticalOffset - per_height);
        }
    }

    private void Editor_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (Linenumber.Children.Count != Editor.LineCount)
        {
            if (Linenumber.Children.Count < Editor.LineCount)
            {
                for (int i = Linenumber.Children.Count; i < Editor.LineCount; i  )
                {
                    TextBlock Linecode = new TextBlock()
                    {
                        Height = Line_Height,
                        Width = Linecode_Width - Linecode_Interval,
                        TextAlignment = TextAlignment.Right,
                        FontFamily = fontFamily,
                        FontSize = Fontsize,
                        Foreground = new SolidColorBrush(LinecodeColor),
                        Background = Brushes.Transparent
                    };

                    Canvas.SetTop(Linecode, i * Line_Height);
                    Linenumber.Children.Add(Linecode);
                }
            }
            else
            if (Linenumber.Children.Count > Editor.LineCount)
            {
                for (int i = Linenumber.Children.Count-1 ; i > Editor.LineCount - 1; i--)
                {
                    Linenumber.Children.RemoveAt(i);
                }
            }

            for (int i = 0; i < Linenumber.Children.Count; i  )
            {
                (Linenumber.Children[i] as TextBlock).Text = "";
            }

            for (int i = 0; i < Editor.LineCount; i  )
            {
                (Linenumber.Children[i] as TextBlock).Text = (i   1).ToString();
            }
            Linenumber.Height = Editor.LineCount* Line_Height < Editor_Box.Height? Editor_Box.Height: Editor.LineCount * Line_Height;
        }
    }

    private void Set_Menu()
    {
        if (string.IsNullOrEmpty(Clipboard.GetText()))
        {
            editMenu.SetMenuVisable(2, false);
        }
        else
        {
            editMenu.SetMenuVisable(2, true);
        }
        ////////////////////////////////////////////////
        if (string.IsNullOrEmpty(Editor.Text))
        {
            editMenu.SetMenuVisable(0, false);
            editMenu.SetMenuVisable(1, false);
            editMenu.SetMenuVisable(3, false);
            editMenu.SetMenuVisable(4, false);
        }
        else
        {
            editMenu.SetMenuVisable(3, true);
            if (Editor.SelectedText.Length < 1)
            {
                editMenu.SetMenuVisable(0, false);
                editMenu.SetMenuVisable(1, false);
                editMenu.SetMenuVisable(4, false);
            }
            else
            {
                editMenu.SetMenuVisable(0, true);
                editMenu.SetMenuVisable(1, true);
                editMenu.SetMenuVisable(4, true);
            }
        }
    }

    public void Set_TextBoxDefrag()
    {
        if (editMenu.ShowMenu)
        {
            editMenu.Visibility = Visibility.Hidden;
            editMenu.ShowMenu = false;
        }
        Editor.Select(Editor.SelectionStart, 0);
    }
}