C# 在WPF中,如何绑定到ViewModel并将各种XAML元素绑定到ViewModel';什么方法?
我可以像这样绑定列表框: XAML:C# 在WPF中,如何绑定到ViewModel并将各种XAML元素绑定到ViewModel';什么方法?,c#,wpf,data-binding,xaml,C#,Wpf,Data Binding,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">
<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>
using System.Windows.Controls;
using TestDynamicForm123.ItemTypes;
namespace TestDynamicForm123.Views
{
public partial class ViewCustomers : UserControl
{
public ViewCustomers()
{
InitializeComponent();
DataContext = Customers.GetAll();
}
}
}
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;
}
}
}
查看模型:
<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>
using System.Windows.Controls;
using TestDynamicForm123.ItemTypes;
namespace TestDynamicForm123.Views
{
public partial class ViewCustomers : UserControl
{
public ViewCustomers()
{
InitializeComponent();
DataContext = Customers.GetAll();
}
}
}
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;
}
}
}
XAML中的这两个语法都不起作用:
<ListBox ItemsSource="{Binding GetAll}"/> (returns blank ListBox)
<ListBox ItemsSource="{Binding Source={StaticResource GetAll}}"/> (returns error)
(返回空白列表框)
(返回错误)
您需要使用绑定到方法,但这应该是例外,而不是常规。通常,您的虚拟机只会公开您绑定到的属性,包括一个公开所有相关Customer
s的属性。您需要使用该属性绑定到方法,但这应该是例外,而不是常规。通常,您的VM只会公开您绑定到的属性,包括公开所有相关Customer
s的属性。您需要为视图创建一个视图模型类(CustomerViewModel)。
CustomerViewModel将通过视图(ViewCustomers)可以绑定到的属性公开数据和命令(ICommand接口)。然后可以将ViewCustomers的DataContext设置为CustomerViewModel的实例。
查看以下有关WPF中模型视图ViewModel模式的MSDN文章
您需要为视图创建视图模型类(CustomerViewModel)。 CustomerViewModel将通过视图(ViewCustomers)可以绑定到的属性公开数据和命令(ICommand接口)。然后可以将ViewCustomers的DataContext设置为CustomerViewModel的实例。 查看以下有关WPF中模型视图ViewModel模式的MSDN文章
我认为ObjectDataProvider是需要避免的,另外它似乎不在Silverlight中,所以我想在没有它的情况下下载一些模式,所以我取出它,将我的V绑定到我的VM,暴露数据,绑定它,但它没有显示:我认为ObjectDataProvider是需要避免的,此外,它似乎不在Silverlight中,所以我想在没有它的情况下下载一些模式,所以我取出它,将我的V绑定到我的VM,公开数据,绑定它,但它没有显示: