在文本框中选定的文本 [英] Selected text in textbox

查看:108
本文介绍了在文本框中选定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个网格2列表视图。每个列表视图包含一个文本框,并在这两个列表视图两个文本框有相同的文字。当我选择在任何文本框中的文本的一部分,其他的文本框选择文本以及相同的部分。

Hi i have 2 listviews in a grid. Each listview contains a textbox and both textboxes in both listviews have the same text. When i select part of the text in either textbox, the other textbox will select the same part of the text as well.

可以在2个不同的列表视图两个文本框之间的结合是做了什么?

can binding between two textbox in 2 different listview be done?

推荐答案

由于AngelWPF写道,在选择* -properties没有,所以你不能对他们使用数据绑定依赖项属性。

As AngelWPF writes, the Selection*-properties are not dependency properties so you cannot use databinding on them.

您可以做虽然什么,是添加自己的有取代原有的性能依赖属性TextBox的subcalss。这些可以被实现为使用相同的名称作为原始特性经常依赖属性,但它们的定义必须的 公开新的 以代替原件

What you can do though, is to add your own subcalss of the TextBox that has dependency properties that replace the original properties. These can be implemented as regular dependency properties using the same names as the original properties, but the definition of them must be public new to replace the originals.

我不会在这里发布一个完整的代码示例(太多的代码,我没有它在此计算机上),但你可以做类似如下:

I will not post an entire code sample here (too much code and I don't have it on this computer), but you can do something like the following:

public class BindableSelectionTextBox : TextBox
{
  // Defines the dependency property as normal
  public static readonly DependencyProperty SelectedTextProperty = 
    DependencyProperty.RegisterAttached(SelectedText, typeof(string),    
      typeof(BindableSelectionTextBox),
      new FrameworkPropertyMetadata("", SelectedTextPropertyChanged));

  private static void SelectedTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  {
    var textBox = (TextBox)d;
    textBox.SelectedText = (string)e.NewValue;
  }

  public new string SelectedText
  {
    get { return (string)GetValue(SelectedTextProperty); }
    set 
    { 
      if(value != SelectedText) 
      {
         SetValue(SelectedTextProperty, value); 
      }
    }
   }

  public BindableSelectionTextBox()
  {
    SelectionChanged += OnSelectionChanged;
  }

  private void OnSelectionChanged(object sender, RoutedEventArgs e)
  {
    SelectedText = base.SelectedText;
  }
}

现在,你必须重复此为 SelectionStart SelectionLength 的属性,你应该做的。

Now, you must repeat this for the SelectionStart and SelectionLength properties and you should be done.

这篇关于在文本框中选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆