2012-03-03 83 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,具體取決於您要實現的目標。