Wpf 为什么此视图未正确绑定到此ViewModel?
我正在尝试在没有DataObjectProvider的情况下将视图绑定到视图模型 下面的代码运行时没有错误,但我的列表框是空的 据我所知,我正确地:Wpf 为什么此视图未正确绑定到此ViewModel?,wpf,xaml,data-binding,mvvm,viewmodel,Wpf,Xaml,Data Binding,Mvvm,Viewmodel,我正在尝试在没有DataObjectProvider的情况下将视图绑定到视图模型 下面的代码运行时没有错误,但我的列表框是空的 据我所知,我正确地: 将视图的DataContext设置为ViewModel本身(DataContext=new CustomerViewModel();) 公开ViewModel中的customer对象(publicstaticobserveCollectiongetAll()) 将列表框绑定到客户对象(ItemsSource=“{Binding GetAll}”)
- 将视图的DataContext设置为ViewModel本身(
)DataContext=new CustomerViewModel();
- 公开ViewModel中的customer对象(
)publicstaticobserveCollectiongetAll()
- 将列表框绑定到客户对象(
)ItemsSource=“{Binding GetAll}”
<UserControl x:Class="TestDynamicForm123.View.CustomersView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
<Style x:Key="allCustomersListBox" TargetType="ListBox">
<Setter Property="BorderThickness" Value="0"/>
</Style>
<DataTemplate x:Key="allCustomersDataTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<StackPanel Style="{StaticResource viewWrappingStackPanel}">
<ListBox ItemsSource="{Binding GetAll}"
ItemTemplate="{StaticResource allCustomersDataTemplate}"
Style="{StaticResource allCustomersListBox}">
</ListBox>
</StackPanel>
</UserControl>
using System.Windows.Controls;
using TestDynamicForm123.ViewModel;
namespace TestDynamicForm123.View
{
public partial class CustomersView : UserControl
{
public CustomersView()
{
InitializeComponent();
DataContext = new CustomersViewModel();
}
}
}
using System.Collections.ObjectModel;
namespace TestDynamicForm123.ViewModel
{
public class CustomersViewModel
{
public static ObservableCollection<Customer> GetAll()
{
return Customer.GetAll();
}
}
}
CustomerViewModel.cs:
<UserControl x:Class="TestDynamicForm123.View.CustomersView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
<Style x:Key="allCustomersListBox" TargetType="ListBox">
<Setter Property="BorderThickness" Value="0"/>
</Style>
<DataTemplate x:Key="allCustomersDataTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<StackPanel Style="{StaticResource viewWrappingStackPanel}">
<ListBox ItemsSource="{Binding GetAll}"
ItemTemplate="{StaticResource allCustomersDataTemplate}"
Style="{StaticResource allCustomersListBox}">
</ListBox>
</StackPanel>
</UserControl>
using System.Windows.Controls;
using TestDynamicForm123.ViewModel;
namespace TestDynamicForm123.View
{
public partial class CustomersView : UserControl
{
public CustomersView()
{
InitializeComponent();
DataContext = new CustomersViewModel();
}
}
}
using System.Collections.ObjectModel;
namespace TestDynamicForm123.ViewModel
{
public class CustomersViewModel
{
public static ObservableCollection<Customer> GetAll()
{
return Customer.GetAll();
}
}
}
使用System.Collections.ObjectModel;
命名空间TestDynamicForm123.ViewModel
{
公共类CustomerViewModel
{
公共静态ObservableCollection GetAll()
{
return Customer.GetAll();
}
}
}
客户。cs:(型号)
使用System.Collections.ObjectModel;
命名空间TestDynamicForm123.ViewModel
{
公共类客户
{
公共int Id{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共整数{get;set;}
公共静态ObservableCollection GetAll()
{
ObservableCollection客户=新ObservableCollection();
添加(新客户(){FirstName=“Jay”,LastName=“Anders”,Age=34});
添加(新客户(){FirstName=“Jim”,LastName=“Smith”,Age=23});
添加(新客户(){FirstName=“John”,LastName=“Jones”,Age=22});
添加(新客户(){FirstName=“Sue”,LastName=“Anders”,Age=21});
添加(新客户(){FirstName=“Chet”,LastName=“Rogers”,年龄=35});
添加(新客户(){FirstName=“James”,LastName=“Anders”,Age=37});
返回客户;
}
}
}
问题在于CustomerViewModel
.GetAll()是一个静态方法。绑定仅适用于实例属性。您应该将GetAll()
的实现移动到构造函数中,然后将集合作为类的实例属性公开。问题在于CustomerViewModel
.GetAll()是一个静态方法。绑定仅适用于实例属性。您应该将GetAll()
的实现移动到构造函数中,然后将集合作为类的实例属性公开。问题在于,当CustomerViewModel中的GetAll是静态函数而不是实例属性时,您正试图绑定到CustomerViewModel.GetAll()
将静态函数更改为类似以下内容(在CustomServiceWModel中):
public observetecollection GetAll
{
获取{return Customer.GetAll();}
}
问题在于,当CustomerViewModel中的GetAll是静态函数而不是实例属性时,您正试图绑定到CustomerViewModel.GetAll()
将静态函数更改为类似以下内容(在CustomServiceWModel中):
public observetecollection GetAll
{
获取{return Customer.GetAll();}
}
是否尝试在Customer.GetAll()方法中放置断点以查看是否调用它?是否尝试在Customer.GetAll()方法中放置断点以查看是否调用它?您可能希望创建一个singleton对象来存储Customer中的所有Customer对象。。。如果多次读取GetAll,它将在每次调用时返回新的客户集合。另一个选择是一次获得客户并保存他们。然后,您可以从GetAll属性返回这些客户。您可能希望创建一个singleton对象以将所有客户对象存储在customer。。。如果多次读取GetAll,它将在每次调用时返回新的客户集合。另一个选择是一次获得客户并保存他们。然后可以从GetAll属性返回这些客户。