DataBind ListBox SelectedItem为RichTextBox

时间:2015-10-22 17:07:41

标签: c# wpf

我有一个填充ListBox的对象,当我点击一个项目将其内容写入RichTextBox时,我想要这样:

标题:bla bla

艺术家:bla bla bla

类型:......

输入:...

这是对象:

public class Multimedia : INotifyPropertyChanged {
    public enum MediaType {
        CD,
        DVD
    };
    public string _title { get; private set; }
    public string _artist { get; private set; }
    public string _genre { get; private set; }
    public MediaType _type { get; private set; }

    public event PropertyChangedEventHandler PropertyChanged;
}

这是多媒体对象的集合:

public class MultiMediaList : ObservableCollection<Multimedia> {
    public MultiMediaList() {

    }
}

MainWindows类

        Multimedia m1 = new Multimedia("Play", "Moby", "Techno", Multimedia.MediaType.CD);
        Multimedia m2 = new Multimedia("18", "Moby", "Techno", Multimedia.MediaType.CD);
        Multimedia m3 = new Multimedia("The Inevitable End", "Royksopp", "Electro", Multimedia.MediaType.CD);

        multimediaList.Add(m1);
        multimediaList.Add(m2);
        multimediaList.Add(m3);

        List<Multimedia> list = new List<Multimedia>();
        list.Add(m1);
        list.Add(m2);
        list.Add(m3);



        DataContext = multimediaList;
        InitializeComponent();
        listBox1.ItemsSource = multimediaList;

和XAML:

    <Grid Name="grid1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <ListBox Grid.Column="0" Name="listBox1">

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="0,2">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="100" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding _artist}" />

                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <GridSplitter Name="gridSplitter1" Margin="0" Width="5" Grid.Column="1" HorizontalAlignment="Left" />

        <RichTextBox Grid.Column="2" Name="richTextBox1" DataContext="{Binding ElementName=listBox1, Path=SelectedItem.Name }"/>


    </Grid>

1 个答案:

答案 0 :(得分:1)

使用文本块,使用正确的绑定路径

可以轻松实现
public class RichTextBoxHelper
{
    #region Text

    /// <summary>
    /// Text Attached Dependency Property
    /// </summary>
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.RegisterAttached("Text", typeof(string), typeof(RichTextBoxHelper),
            new FrameworkPropertyMetadata((string)null,
                new PropertyChangedCallback(OnTextChanged)));

    /// <summary>
    /// Gets the Text property.  This dependency property 
    /// indicates ....
    /// </summary>
    public static string GetText(DependencyObject d)
    {
        return (string)d.GetValue(TextProperty);
    }

    /// <summary>
    /// Sets the Text property.  This dependency property 
    /// indicates ....
    /// </summary>
    public static void SetText(DependencyObject d, string value)
    {
        d.SetValue(TextProperty, value);
    }

    /// <summary>
    /// Handles changes to the Text property.
    /// </summary>
    private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        RichTextBox textBox = (RichTextBox)d;
        if (e.NewValue != null)
        {
            textBox.Document.Blocks.Clear();
            textBox.Document.Blocks.Add(new Paragraph(new Run(e.NewValue.ToString())));
        }
    }

    #endregion

    /// <summary>
    /// Returns the Text from a FlowDocument
    /// </summary>
    /// <param name="document">The document to get the text from</param>
    /// <returns>Returns a string with the text of the flow document</returns>
    public static string GetText(FlowDocument document)
    {
        return new TextRange(document.ContentStart, document.ContentEnd).Text;
    }

}

使用富文本框有点棘手,你可以使用Marlon Grech的RichTextBoxHelper的修改版本,如下所示(注意xaml中的本地命名空间引用)

RichTextBoxHelper.cs:

<Window x:Class="RichTextBoxDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:RichTextBoxDemo"
    Title="Window1" Height="300" Width="300">
    <Grid Name="grid1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <ListBox Grid.Column="0" x:Name="listBox1">

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="0,2">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="100" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding _artist}" />

                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <GridSplitter Name="gridSplitter1" Margin="0" Width="5" Grid.Column="1" HorizontalAlignment="Left" />
        <RichTextBox x:Name="textBox" Grid.Column="2" SpellCheck.IsEnabled="True" 
            local:RichTextBoxHelper.Text="{Binding ElementName=listBox1, Path=SelectedItem._title}"     />
    </Grid>
</Window>

XAML:

{{1}}

截图:

Select List Item