我正在尝试将我的视图绑定到我的视图模型而不使用 DataObjectProvider。

下面的代码运行没有错误, ,但我的列表框是 空的.

据我所知,我是正确的:

  • 将 View 的 DataContext 设置为 ViewModel 本身(DataContext = new CustomersViewModel();)
  • 在 ViewModel 中公开客户对象(public static ObservableCollection<Customer> GetAll())
  • 将 ListBox 绑定到客户对象 (ItemsSource="{Binding GetAll}")

我在这里缺少哪一小段语法?

客户视图.xaml:

<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>

CustomersView.xaml.cs:

using System.Windows.Controls;
using TestDynamicForm123.ViewModel;

namespace TestDynamicForm123.View
{
    public partial class CustomersView : UserControl
    {
        public CustomersView()
        {
            InitializeComponent();

            DataContext = new CustomersViewModel();
        }
    }
}

客户视图模型.cs:

using System.Collections.ObjectModel;

namespace TestDynamicForm123.ViewModel
{
    public class CustomersViewModel
    {
        public static ObservableCollection<Customer> GetAll()
        {
            return Customer.GetAll();
        }
    }
}

客户.cs: (模型)

using System.Collections.ObjectModel;

namespace TestDynamicForm123.ViewModel
{
    public class Customer
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }

        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;
        }

    }
}
有帮助吗?

解决方案

问题是您尝试绑定到 CustomerViewModel.GetAll(),而 CustomerViewModel 中的 GetAll 是静态函数而不是实例属性。

将静态函数更改为如下所示(在 CustomerViewModel 中):

public ObservableCollection<Customer> GetAll
{
    get { return Customer.GetAll(); }
}

其他提示

问题是 CustomersViewModel .GetAll() 是一个静态方法。绑定仅适用于实例属性。你应该移动实施 GetAll() 到构造函数,然后将集合公开为类的实例属性。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top