【发布时间】:2011-01-08 23:43:46
【问题描述】:
当用户在richTextbox中写一些文本时,我需要从richTextBox获取最后三个字符。
我在扩展 WPF 工具包中的 RichTextBox 的 Text 属性上绑定属性。
public string RtbText
{
get { return _rtbText; }
set
{
_rtbText = value;
NotifyPropertyChanged("RtbText");
}
}
我使用 Reactive Extensions for .NET (Rx) 并在属性 RtbText 上创建 Observer
Observable.FromEvent<PropertyChangedEventArgs>(this, "PropertyChanged")
.Where(e => e.EventArgs.PropertyName == "RtbText")
.Select(_ => this.RtbText)
.Where(text => text.Length > 1)
.Do(AddSmiles)
.Throttle(TimeSpan.FromSeconds(1))
.Subscribe(GetLastThreeChars);
private void GetLastThreeChars(string text)
{
if (text.Length > 3)
{
string lastThreeChars = text.Substring(text.Length - 2, text.Length);
}
}
但是如果我开始输入richTextBox,我会得到这个异常:
索引和长度必须引用字符串中的位置。
参数名称:长度在 System.String.InternalSubStringWithChecks(Int32 startIndex,Int32 长度,布尔 fAlwaysCopy)
在 System.String.Substring(Int32 startIndex, Int32 长度)
在 C:\Users\Jan\Documents\Visual Studio 2010\Projects\C#\Pokec_Messenger\ver.beta\IoC.Get\Pokec_Messenger\ver.beta\Pokec_Messenger\ 中的 WpfApplication1.MainWindow.GetLastThreeChars(字符串文本) WpfApplication1\MainWindow.xaml.cs:第 97 行
在 System.Linq.Observable.c_DisplayClass389`1.c_DisplayClass38b.b_388(TSource x)
【问题讨论】:
标签: wpf string richtextbox system.reactive