2012-03-03 80 views
0

我想在列表视图中绑定RichTextBox无济于事。如果我不将RichTextBox包装在列表视图中,只将它分配给类,它就可以工作。但只要我尝试分配给列表视图,它不会显示文本,但会显示正确的项目数。列表框绑定中的扩展WPF工具包富文本框

例子:

<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框中未显示任何文本。

谢谢...

+0

'datas'不是一个正确的词。数据已经是复数了。但可能这不是你的问题的答案。 – Snowbear 2012-03-03 10:41:55

回答

1

您的结合模式是OneWayToSource。在这种情况下源是Data实例,因此您正在将数据从RichTextBox推回到您的Notes财产,但不是其他方向

更改您的绑定模式为OneWayTwoWay,具体取决于您要实现的目标。