WPF应用程序框架是否绑定到模型?

发布于 2024-09-19 00:19:27 字数 4466 浏览 5 评论 0原文

今天,我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聊慰 2024-09-26 00:19:27

通过 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文