<Grid>
<StackPanel>
<TextBox x:Name="txtHostId" Margin="5" Text="{Binding ElementName=lstHost, Path=SelectedItem.DbId}"></TextBox>
<ListBox x:Name="lstHost" Height="100" Margin="5"></ListBox>
</StackPanel>
</Grid>
public MainWindow()
{
InitializeComponent();
List<DbInfo> dbInfos = new List<DbInfo>();
for (int i = 0; i < 10; i++)
{
DbInfo dbInfo = new DbInfo();
dbInfo.DbId = i;
dbInfo.HostId = i;
dbInfo.Remark = "R"+i.ToString();
dbInfos.Add(dbInfo);
}
lstHost.ItemsSource = dbInfos;
//代码绑定
//lstHost.DisplayMemberPath = "Remark";
//DataContext = this;
//Binding binding = new Binding("SelectedItem.DbId") { Source = this.lstHost };
//txtHostId.SetBinding(TextBox.TextProperty, binding);
}
代码后台绑定和前台xmal绑定。