【发布时间】:2017-02-03 13:36:56
【问题描述】:
所以我想要实现的是将当前项目从 ItemsSource 绑定到 Comobox 中的 SelectedItem 我希望下面的(一个简化的示例)代码能够展示我想要的。
public class book : INotifyPropertyChanged{
private string _title;
private string _author;
public book(){
this.Title = "";
this.
}
public string Title{
get{return _title;}
set{
_title = value;
NotifyPropertyChanged("Title");
}
}
public string Author{
get{return _author;}
set{
_author = value;
NotifyPropertyChanged("Author");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
ThisViewModel.cs 代码:
public class ThisViewModel{
private ObservableCollection<Book> _book;
private List<Book> _existedBooks;
public ObservableCollection<Book> Book{
get{return _book;}
set{_book = value;}
}
public ThisViewModel(){
Books = new ObservableCollection<Book>();
Books.Add(new Book())
}
public List<Book> ExistedBooks{
get{return _existedBooks;}
set{_existedBooks = value;}
}
}
ThisView.xaml.cs 的代码隐藏:
public partial class ThisView{
public ThisView(){
InitializeComponent();
this.DataContext = new ThisViewModel();
}
}
XAML 代码 ThisView.xaml:
<ItemsControl ItemsSource="{Binding Path=Book}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=ExistedBooks}"
DisplayMemberPath="Title"
SelectedItem="{Binding <HERE IS MY PROBLEM>}"
></ComboBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
如何将 selectedItem(现有书籍之一)绑定到 ItemsControl 当前项目(书籍)。
希望我的观点足够清楚。 感谢您的宝贵时间。
【问题讨论】:
标签: c# wpf xaml itemscontrol