【问题标题】:Extended WPF Toolkit Rich Text Box in ListBox BindingListBox 绑定中的扩展 WPF 工具包富文本框
【发布时间】: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


【解决方案1】:

您的绑定模式是OneWayToSource。在这种情况下,源是 Data 实例,因此您将数据从 RichTextBox 推送回您的 Notes 属性,但不是另一个方向

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

【讨论】:

    猜你喜欢
    • 2012-11-16
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    相关资源
    最近更新 更多