为什么这个视图没有正确绑定到这个ViewModel? [英] Why is this View not correctly binding to this ViewModel?

查看:119
本文介绍了为什么这个视图没有正确绑定到这个ViewModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下代码运行时没有错误,但我的列表框是



据我所知,我正确地说:




  • 将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& gt;
< UserControl.Resources>
< Style x:Key =allCustomersListBoxTargetType =ListBox>
< Setter Property =BorderThicknessValue =0/>
< / Style>
< DataTemplate x:Key =allCustomersDataTemplate>
< StackPanel Orientation =Horizo​​ntal>
< 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:

 使用System.Windows.Controls; 
使用TestDynamicForm123.ViewModel;

命名空间TestDynamicForm123.View
{
public partial class CustomersView:UserControl
{
public CustomersView()
{
InitializeComponent ();

DataContext = new CustomersViewModel();
}
}
}

CustomersViewModel.cs :

  using System.Collections.ObjectModel; 

命名空间TestDynamicForm123.ViewModel
{
public class CustomersViewModel
{
public static ObservableCollection< Customer> GetAll()
{
return Customer.GetAll();
}
}
}

Customer.cs :(模型)

 使用System.Collections.ObjectModel; 

命名空间TestDynamicForm123.ViewModel
{
public class Customer
{
public int Id {get;组; }
public string FirstName {get;组; }
public string LastName {get;组; }
public int Age {get;组; }

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});
回报客户;
}

}
}


解决方案

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



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

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


I'm trying to bind my view to my view model without DataObjectProvider.

The following code runs without an error, but my ListBox is empty.

As far as I can tell, I correctly:

  • set the View's DataContext to the ViewModel itself (DataContext = new CustomersViewModel();)
  • expose the customer objects in the ViewModel (public static ObservableCollection<Customer> GetAll())
  • bind the ListBox to the customer objects (ItemsSource="{Binding GetAll}")

What little piece of syntax am I missing here?

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: (model)

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

    }
}

解决方案

The problem is that you are trying to bind to CustomerViewModel.GetAll(), when GetAll in CustomerViewModel is a static function not an instance property.

Change the static function to something like this (in CustomerViewModel):

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

这篇关于为什么这个视图没有正确绑定到这个ViewModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆