从Observable Collection或List()获取项目

时间:2012-09-14 10:18:33

标签: c# wpf xaml data-binding microsoft-metro

我有一个名为NotesList的ListBox。我有一个名为noteList的ObservableCollection,我有一个名为NoteContents的TextBox。

在我的ObservableCollection中,我为一些项设置了Filename和Contents属性,然后它被添加(绑定)到我的ListBox。

但是现在,我想(当我点击一个按钮时),显示在NoteContents TextBox中选择的ListBox项目的“内容”。

我该怎么做?

我目前有:

private void NotesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    NoteContents.Text = noteList.Where(x => x.Filename.Contains(NotesList.SelectedValue.ToString())).FirstOrDefault().Contents;
}

1 个答案:

答案 0 :(得分:2)

您可以在没有按钮点击的情况下执行此操作,只需绑定如下:

<ListBox Name="NotesList" ItemsSource="{Binding YourObservableCollection}">
    <!--Your bindings here-->
</ListBox>
<TextBox Text="{Binding ElementName=NotesList, Path=SelectedItem.Contents}" />
相关问题