【问题标题】:Why is this View not correctly binding to this ViewModel?为什么这个 View 没有正确绑定到这个 ViewModel?
【发布时间】:2010-10-22 13:50:34
【问题描述】:

我正在尝试在没有 DataObjectProvider 的情况下将我的视图绑定到我的视图模型。

以下代码运行没有错误,但我的 ListBox 为空

据我所知,我是正确的:

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

我在这里遗漏了什么小语法?

CustomersView.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();
        }
    }
}

CustomersViewModel.cs:

using System.Collections.ObjectModel;

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

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

    }
}

【问题讨论】:

  • 您是否尝试在 Customer.GetAll() 方法中放置断点以查看它是否被调用?

标签: wpf xaml data-binding mvvm viewmodel


【解决方案1】:

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

将静态函数更改为类似这样(在 CustomerViewModel 中):

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

【讨论】:

  • 您可能想要创建一个单例对象来存储客户中的所有客户对象...如果多次读取 GetAll,它将在每次调用时返回一个新的客户集合。另一种选择是获得客户一次并保存他们。然后,您可以从 GetAll 属性返回这些客户。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-28
    • 2013-09-16
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 2010-11-14
    相关资源
    最近更新 更多