WPF应用程序框架是否绑定到模型?
今天,我从 codeplex 稍微检查了一下 WAP,看到了这个:
BookListView.xaml:
<ListView ItemsSource="{Binding Books}" SelectedItem="{Binding SelectedBook}"
SelectionChanged="ListViewSelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Title}" Header="{x:Static p:Resources.Title}" Width="250"/>
<GridViewColumn DisplayMemberBinding="{Binding Author}" Header="{x:Static p:Resources.Author}" Width="150"/>
<GridViewColumn DisplayMemberBinding="{Binding PublishDate, StringFormat=d}" Header="{x:Static p:Resources.PublishDate}" Width="100"/>
</GridView>
</ListView.View>
UPDATE:
[Export(typeof(IBookListView))]
public partial class BookListView : UserControl, IBookListView
{
public BookListView()
{
InitializeComponent();
}
private BookListViewModel ViewModel { get { return DataContext as BookListViewModel; } }
private void ListViewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (Book book in e.RemovedItems)
{
ViewModel.SelectedBooks.Remove(book);
}
foreach (Book book in e.AddedItems)
{
ViewModel.SelectedBooks.Add(book);
}
}
}
public class BookListViewModel : ViewModel<IBookListView>
{
private readonly IEnumerable<Book> books;
private readonly ObservableCollection<Book> selectedBooks;
private Book selectedBook;
private ICommand addNewCommand;
private ICommand removeCommand;
public BookListViewModel(IBookListView view, IEnumerable<Book> books)
: base(view)
{
if (books == null) { throw new ArgumentNullException("books"); }
this.books = books;
this.selectedBooks = new ObservableCollection<Book>();
}
public IEnumerable<Book> Books { get { return books; } }
public ObservableCollection<Book> SelectedBooks
{
get { return selectedBooks; }
}
public Book SelectedBook
{
get { return selectedBook; }
set
{
if (selectedBook != value)
{
selectedBook = value;
RaisePropertyChanged("SelectedBook");
}
}
}
public ICommand AddNewCommand
{
get { return addNewCommand; }
set
{
if (addNewCommand != value)
{
addNewCommand = value;
RaisePropertyChanged("AddNewCommand");
}
}
}
public ICommand RemoveCommand
{
get { return removeCommand; }
set
{
if (removeCommand != value)
{
removeCommand = value;
RaisePropertyChanged("RemoveCommand");
}
}
}
}
BookController: Here the books are fetched from Entity Framework/database
public void Initialize()
{
bookViewModel.LendToCommand = lendToCommand;
bookViewModel.PropertyChanged += BookViewModelPropertyChanged;
IBookListView bookListView = container.GetExportedValue<IBookListView>();
bookListViewModel = new BookListViewModel(bookListView, entityService.Books);
bookListViewModel.AddNewCommand = addNewCommand;
bookListViewModel.RemoveCommand = removeCommand;
bookListViewModel.PropertyChanged += BookListViewModelPropertyChanged;
shellViewModel.BookListView = bookListViewModel.View;
shellViewModel.BookView = bookViewModel.View;
bookListViewModel.SelectedBook = bookListViewModel.Books.FirstOrDefault();
}
and those books are displayed bookListViewModel = new BookListViewModel(bookListView,entityService.Books); 到 BookListViewModel,它被设置为 BookListView.xaml 绑定到书籍的 DataContext。
我很震惊!它们直接绑定到模型属性,我说得对吗?
today I examinted WAP from codeplex a bit and saw this:
BookListView.xaml:
<ListView ItemsSource="{Binding Books}" SelectedItem="{Binding SelectedBook}"
SelectionChanged="ListViewSelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Title}" Header="{x:Static p:Resources.Title}" Width="250"/>
<GridViewColumn DisplayMemberBinding="{Binding Author}" Header="{x:Static p:Resources.Author}" Width="150"/>
<GridViewColumn DisplayMemberBinding="{Binding PublishDate, StringFormat=d}" Header="{x:Static p:Resources.PublishDate}" Width="100"/>
</GridView>
</ListView.View>
UPDATE:
[Export(typeof(IBookListView))]
public partial class BookListView : UserControl, IBookListView
{
public BookListView()
{
InitializeComponent();
}
private BookListViewModel ViewModel { get { return DataContext as BookListViewModel; } }
private void ListViewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (Book book in e.RemovedItems)
{
ViewModel.SelectedBooks.Remove(book);
}
foreach (Book book in e.AddedItems)
{
ViewModel.SelectedBooks.Add(book);
}
}
}
public class BookListViewModel : ViewModel<IBookListView>
{
private readonly IEnumerable<Book> books;
private readonly ObservableCollection<Book> selectedBooks;
private Book selectedBook;
private ICommand addNewCommand;
private ICommand removeCommand;
public BookListViewModel(IBookListView view, IEnumerable<Book> books)
: base(view)
{
if (books == null) { throw new ArgumentNullException("books"); }
this.books = books;
this.selectedBooks = new ObservableCollection<Book>();
}
public IEnumerable<Book> Books { get { return books; } }
public ObservableCollection<Book> SelectedBooks
{
get { return selectedBooks; }
}
public Book SelectedBook
{
get { return selectedBook; }
set
{
if (selectedBook != value)
{
selectedBook = value;
RaisePropertyChanged("SelectedBook");
}
}
}
public ICommand AddNewCommand
{
get { return addNewCommand; }
set
{
if (addNewCommand != value)
{
addNewCommand = value;
RaisePropertyChanged("AddNewCommand");
}
}
}
public ICommand RemoveCommand
{
get { return removeCommand; }
set
{
if (removeCommand != value)
{
removeCommand = value;
RaisePropertyChanged("RemoveCommand");
}
}
}
}
BookController: Here the books are fetched from Entity Framework/database
public void Initialize()
{
bookViewModel.LendToCommand = lendToCommand;
bookViewModel.PropertyChanged += BookViewModelPropertyChanged;
IBookListView bookListView = container.GetExportedValue<IBookListView>();
bookListViewModel = new BookListViewModel(bookListView, entityService.Books);
bookListViewModel.AddNewCommand = addNewCommand;
bookListViewModel.RemoveCommand = removeCommand;
bookListViewModel.PropertyChanged += BookListViewModelPropertyChanged;
shellViewModel.BookListView = bookListViewModel.View;
shellViewModel.BookView = bookViewModel.View;
bookListViewModel.SelectedBook = bookListViewModel.Books.FirstOrDefault();
}
and exactly those books are passed
bookListViewModel = new BookListViewModel(bookListView, entityService.Books);
to the BookListViewModel which is set as DataContext of the BookListView.xaml binding to books.
I was shocked!!! Am I right that they directly bind to the models properties?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过 ViewModel 上的属性直接绑定到模型是完全合法的。
如果 ViewModel 需要处理视图,则只需重新实现 ViewModel 上的属性,否则由您选择。
请查看此内容,了解 Karl Shifflet 的看法MVVM 应用层。
It is perfectly legitimate to bind directly to the model via a property on the ViewModel.
You would only have to reimplement a property on the ViewModel if it required processing for the View, otherwise it is your choice.
Take a look at this for Karl Shifflet's take on MVVM application layers.