【发布时间】:2012-03-03 23:54:40
【问题描述】:
我试图在列表视图中绑定 RichTextBox 无济于事。如果我不将 RichTextBox 包装在列表视图中并且只将其分配给类,它就可以正常工作。但是,一旦我尝试分配给列表视图,它就不会显示文本,但会显示正确数量的项目。
例子:
<ListBox Name="lstBook" ItemsSource="{Binding}" Width="auto">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<extToolkit:RichTextBox Margin="5"
BorderBrush="Gray" Padding="1"
Text="{Binding Path=Notes, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Height="100"
Width="425">
<extToolkit:RichTextBox.TextFormatter>
<extToolkit:RtfFormatter />
</extToolkit:RichTextBox.TextFormatter>
</extToolkit:RichTextBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
C#代码
public class Data: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
private string _notes = "";
public string Notes
{
get { return _notes; }
set { _notes = value; OnPropertyChanged("Notes"); }
}
public override string ToString()
{
return Notes;
}
}
要填充的代码:
ObservableCollection<Data> datas = new ObservableCollection<Data>();
Data d = new Data
{
Notes =
@"{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs18\f2\cf0 \cf0\ql{\f2 {\ltrch This is the }{\b\ltrch RichTextBox}\li0\ri0\sa0\sb0\fi0\ql\par}}}"
};
datas.Add(d);
d = new Data();
d.Notes = @"{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs18\f2\cf0 \cf0\ql{\f2 {\ltrch This is the }{\b\ltrch RichTextBox}\li0\ri0\sa0\sb0\fi0\ql\par}}}";
datas.Add(d);
lstBook.ItemsSource = datas;
我错过了什么?列表框显示两条记录,但 Rtf 框中不显示任何文本。
谢谢...
【问题讨论】:
-
datas不是一个正确的词。data已经是复数形式了。但这可能不是您问题的答案。
标签: c# wpf data-binding richtextbox observablecollection