如何在LongListSelector中访问RichTextBox

本文关键字:访问 RichTextBox LongListSelector | 更新日期: 2023-09-27 18:20:35

我需要在LongListSelector中使用RichTextBox,如下所示:

<phone:LongListSelector Name="myLLS">
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <TextBlock Text="{Binding Name}" />
                <TextBlock Text="{Binding Surname}" />
                <RichTextBox IsReadOnly="True" />
            </StackPanel>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>

C#代码:

ObservableCollection<ListItem> listItems = new ObservableCollection<ListItem>();
public MainPage()
    {
        InitializeComponent();
        myLLS.ItemsSource = trainStations;
        listItems.Add(new ListItem("John", "Smith", "Some big and formatted text 1"));
        listItems.Add(new ListItem("Bill", "Dixon", "Some big and formatted text 2"));
        listItems.Add(new ListItem("Ralph", "Watson", "Some big and formatted text 3"));
    }
public class ListItem
    {
        private string _name;
        private string _surname;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        public string Surname
        {
            get { return _surname; }
            set { _surname = value; }
        }
        public ListItem(string name, string surname, string description)
        {
            this.Name = name;
            this.Surname = surname;
            // How to set content to my RichTextBox here?
        }
    }

在这里使用TextBlock是没有问题的。但我需要将描述的文本设置为RichTextBox。如何制作?

如何在LongListSelector中访问RichTextBox

使用绑定很难实现基本的richtextbox,但ExtendedWpfToolkit创建了一个非常好的替代方案http://wpftoolkit.codeplex.com/wikipage?title=RichTextBox&referringTitle=主页