将组合框中所选对象的属性绑定到WPF中的TextBox

Sta*_*ked 2 wpf binding combobox textbox mvvm

我正在使用MVVM加载文本文件并显示其内容.

模型

MyFile.cs有一个NameText//实现INotifyPropertyChanged

MyFileRepository.cs //我加载的文件的集合

视图模型

OpenFileCommand 加载文件并将其添加到_filerepository对象

FileCollection 这与视图绑定

视图

Button 解雇 OpenCommand

ComboBox 显示已加载文件的名称

TextBox 在combobx中显示所选文件的内容

<Button Name="OpenFile" Command="{Binding OpenFileCommand}">
<ComboBox  Name="FilesList" ItemsSource="{Binding Path=FileCollection}" DisplayMemberPath="Name" />
<TextBox Name="FileContent" Text="{Binding the Text of selected file in combobx "/>
Run Code Online (Sandbox Code Playgroud)

如何将在combobx中选择的MyFile的Text属性绑定到TextBox?

Mat*_*ton 7

最简单的方法是元素绑定:

<TextBox Name="FileContent"
         Text="{Binding SelectedItem.Text,ElementName=FilesList} />
Run Code Online (Sandbox Code Playgroud)

所以这是结合的SelectedItem在你FilesList组合框,它(如果一切都为其接线方式,我认为它是)是类型为MyFile的Text属性.