简体   繁体   English

在WPF中,如何绑定到ViewModel并使各种XAML元素绑定到ViewModel的方法?

[英]In WPF how can I bind to the ViewModel and have various XAML elements bind to the ViewModel's methods?

I can bind a ListBox like this: 我可以像这样绑定一个ListBox:

XAML: XAML:

<UserControl x:Class="TestDynamicForm123.Views.ViewCustomers"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel Margin="10">
        <ListBox ItemsSource="{Binding}"/>
    </StackPanel>
</UserControl>

Code Behind: 背后的代码:

using System.Windows.Controls;
using TestDynamicForm123.ItemTypes;

namespace TestDynamicForm123.Views
{
    public partial class ViewCustomers : UserControl
    {
        public ViewCustomers()
        {
            InitializeComponent();

            DataContext = Customers.GetAll();
        }
    }
}

View Model: 查看模型:

using System.Collections.ObjectModel;

namespace TestDynamicForm123.ItemTypes
{
    class Customers : ItemTypes
    {
        protected ObservableCollection<Customer> collection;

        public static ObservableCollection<Customer> GetAll()
        {
            ObservableCollection<Customer> customers = new ObservableCollection<Customer>();
            customers.Add(new Customer() { FirstName = "Jay", LastName = "Anders", Age = 34 });
            customers.Add(new Customer() { FirstName = "Jim", LastName = "Smith", Age = 23 });
            customers.Add(new Customer() { FirstName = "John", LastName = "Jones", Age = 22 });
            customers.Add(new Customer() { FirstName = "Sue", LastName = "Anders", Age = 21 });
            customers.Add(new Customer() { FirstName = "Chet", LastName = "Rogers", Age = 35 });
            customers.Add(new Customer() { FirstName = "James", LastName = "Anders", Age = 37 });
            return customers;
        }
    }
}

But how do I "move it up a level" so I can in the code behind bind to Customers itself and within my XAML bind to Customers various methods , eg GetAll() for a ListBox and GetBest() for another control, etc.? 但是我如何“将其升级”,以便可以在代码中绑定到“客户”本身,然后在我的XAML中绑定到“客户”的各种方法 ,例如,对于列表框的GetAll()和对于另一个控件的GetBest()等?

I tried it with this syntax in the code behind: 我在后面的代码中使用此语法进行了尝试:

DataContext = new Customers();

And these two syntaxes in the XAML but neither work: 而且XAML中的这两种语法都不能正常工作:

<ListBox ItemsSource="{Binding GetAll}"/> (returns blank ListBox)
<ListBox ItemsSource="{Binding Source={StaticResource GetAll}}"/> (returns error)

You need to use ObjectDataProvider to bind to methods, but that should be the exception rather than the norm. 您需要使用ObjectDataProvider绑定到方法,但这应该是例外而不是常规。 Usually your VM would just expose properties that you bind to, including a property that exposes all relevant Customer s. 通常,您的VM只会公开您绑定的属性,包括公开所有相关Customer的属性。

You need to create a view model class (CustomersViewModel) for your view. 您需要为视图创建视图模型类(CustomersViewModel)。 CustomersViewModel will expose the data and the command (ICommand interface) via properties that your view (ViewCustomers) can bind to. CustomersViewModel将通过视图(ViewCustomers)可以绑定到的属性公开数据和命令(ICommand接口)。 You can then set DataContext of ViewCustomers to an instance of CustomersViewModel. 然后,可以将ViewCustomers的DataContext设置为CustomerViewModel的实例。 Check out the following MSDN article on Model-View-ViewModel pattern in WPF. 在WPF中查看有关Model-View-ViewModel模式的以下MSDN文章。

WPF Apps With The Model-View-ViewModel Design Pattern 具有Model-View-ViewModel设计模式的WPF应用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM