ListBox绑定中的扩展WPF工具包富文本框

时间:2012-03-03 10:04:48

标签: c# wpf data-binding richtextbox observablecollection

我试图在列表视图中绑定RichTextBox无济于事。如果我没有将RichTextBox包装在listview中并且只将它分配给类,那么它可以正常工作。但是一旦我尝试分配到列表视图,它就不会显示文本,但会显示正确数量的项目。

示例:

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

...谢谢

1 个答案:

答案 0 :(得分:1)

您的绑定模式为OneWayToSource。这种情况下的来源是Data实例,因此您要将数据从RichTextBox推回到Notes属性,而不是其他方向。< / p>

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

相关问题