1.如何查找RichTextBox内容并选择它?
   WinFrom 中可以用RichTextBox1.Select(pos, keyword.Length);WPF中如何选中查找的内容?2.要做个ListBox的鼠标滑入效果,鼠标滑入到ListBox条目上面,那一条自动选中。
在WinFrom里面是        private void listBox1_MouseMove(object sender, MouseEventArgs e)
        {
            Point m = new Point(e.X, e.Y);
            int index = GetItemAt(this.listBox1, e.X, e.Y);
            if (this.listBox1.SelectedItems.Count > 0 && this.listBox1.SelectedIndex != index)
            {
                this.listBox1.SetSelected(this.listBox1.SelectedIndex, false);
            }            if (index != -1 && this.listBox1.SelectedIndex != index)
            {                this.listBox1.(index, true);
            }
        }        private int GetItemAt(ListBox listBox, int X, int Y)
        {
            int index = -1;
            for (int i = 0; i < listBox.Items.Count; i++)
            {
                System.Drawing.Rectangle r = listBox.GetItemRectangle(i);
                if (r.Contains(new Point(X, Y)))
                {
                    index = i; ;
                    break;
                }
            }
            return index;ListBox里面没有GetItemRectangle这个属性,请问如何实现该效果?谢谢。

解决方案 »

  1.   

    Q1:如果你已经知道Pos和Length,可以这么做:            //设置选择长度
                  int selectLength = 10;            //获取起始,结束的TextPointer
                TextPointer tpStart = richTextBox1.Document.ContentStart;
                TextPointer tpOffset = tpStart.GetPositionAtOffset(selectLength);            //获取Range对象,并做选择
                  TextRange range = richTextBox1.Selection;
                range.Select(tpStart, tpOffset);            //高亮选择
                range.ApplyPropertyValue(TextElement.ForegroundProperty,
    new SolidColorBrush(Colors.Blue));
                range.ApplyPropertyValue(TextElement.FontWeightProperty,
            FontWeights.Bold);
    Q2:你需要让ListBox中的ListBoxItem响应MouseEnter事件,
    因为ListBoxItem也是一个FrameworkElement,能响应路由事件.
    假设你的ListBoxItem事先已经加好,我们遍历加上事件响应            foreach (object item in listBox1.Items)
                {
                    ListBoxItem lbi = item as ListBoxItem;
                    if (lbi != null)
                    {
                        lbi.MouseEnter += delegate
                        {
                            foreach (object obj in listBox1.Items)
                            {
                                ListBoxItem lbItem = obj as ListBoxItem;
                                if(lbItem != null)
                                {
                                    lbItem.IsSelected = lbi == lbItem;
                                }
                            }
                        };
                    }
                }
      

  2.   

    第二个问题直接在Xaml就可以搞定,在模板里设一下IsMouseOver的Trigger,把IsSelected设为True
      

  3.   


    1.我根据一个字符串查询,richTextBox1如何让内容里面所有的改字符串高亮?如何让第一个字符串属于选中状态。2。你的方法不行。试了没用。。
      

  4.   

    刚刚学WPF,XAML里面很多属性都不熟,请问Trigger事件怎么写?
      

  5.   

    1。 你能选择一个,自然也能选择多个字符串,要出现选中效果,可以设置
    选中部分的背景笔刷为一种颜色(蓝,黑),选中部分的字体为高亮色,跟背景笔刷有明显差异即可.
      range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Blue));            range.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.White));2。如果你直接通过ItemSource绑定,是不行的,因为你的ListBox的Items中的对象类型不是ListBoxItem.如果你一个个加ListBoxItem自然没问题.Trigger的例子
      

  6.   

    研究了一下,可以这么处理:
                      ObservableCollection<string> collection = new ObservableCollection<string>();
                    collection.Add("aa");
                    collection.Add("bb");
                    collection.Add("cc");
                    listBox1.ItemsSource = collection;        //需要判断OriginalSource
            void listBox1_MouseMove(object sender, MouseEventArgs e)
            {
                //自动让ListBox获得焦点
                if (!listBox1.IsFocused)
                {
                    listBox1.Focus();
                }            TextBlock tb = e.OriginalSource as TextBlock;
                if (tb != null)
                {
                    //如果绑定的Source都是String,这么处理即可,
                    //否则,转成相应的类型再做赋值
                    listBox1.SelectedItem = tb.Text;
                }
             }
      

  7.   

    感谢你这么热心回复,那个ListBox搞定了。。那个RICHTEXTBOX怎么获得我要查找的字符串的位置?
      

  8.   

                    ObservableCollection<string> collection = new ObservableCollection<string>();
                    collection.Add("aa");
                    collection.Add("bb");
                    collection.Add("cc");
                    listBox1.ItemsSource = collection;        //需要判断OriginalSource
            void listBox1_MouseMove(object sender, MouseEventArgs e)
            {
                //自动让ListBox获得焦点
                if (!listBox1.IsFocused)
                {
                    listBox1.Focus();
                }            TextBlock tb = e.OriginalSource as TextBlock;
                if (tb != null)
                {
                    //如果绑定的Source都是String,这么处理即可,
                    //否则,转成相应的类型再做赋值
                    listBox1.SelectedItem = tb.Text;
                }
             }
      

  9.   

    查找,高亮代码如下:
     private void btnSearch_Click(object sender, RoutedEventArgs e)
            {
                string keyword = "aa";
                foreach (var ret in FindAllMatchedTextRanges(richTextBox1, keyword))
                {
                    ret.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Blue));
                    ret.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.White));
                }
            }        private List<TextRange> FindAllMatchedTextRanges(RichTextBox rtb, string keyword)
            {
                List<TextRange> trList = new List<TextRange>();
                //设置文字指针为Document初始位置
                TextPointer position = rtb.Document.ContentStart;
                while (position != null)
                {
                    //向前搜索,需要内容为Text
                    if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
                    {
                        //拿出Run的Text
                        string text = position.GetTextInRun(LogicalDirection.Forward);
                        //可能包含多个keyword,做遍历查找
                        int index = 0;
                        while (index < text.Length)
                        {
                            index = text.IndexOf(keyword, index);
                            if (index == -1)
                            {
                                break;
                            }
                            else
                            {
                                //添加为新的Range
                                TextPointer start = position.GetPositionAtOffset(index);
                                TextPointer end = start.GetPositionAtOffset(keyword.Length);
                                trList.Add(new TextRange(start, end));
                                index += keyword.Length;
                            }
                        }
                    }
                    //文字指针向前偏移
                    position = position.GetNextContextPosition(LogicalDirection.Forward);
                }
                return trList;
            }
      

  10.   


    , 学习了,之前只想到了到RichTextBox.Document中去找这个TextPointer,原来是这么用的