2010-05-17 56 views
5

我正在使用MVVm工具包在WPF中编写应用程序,并且存在连接视图模型和视图的问题。如何设置用户控件的数据环境

该模型使用ado.net实体框架创建。

视图模型:

public class CustomerViewModel 
    { 
     private Models.Customer customer; 
     //constructor 
     private ObservableCollection<Models.Customer> _customer = new ObservableCollection<Models.Customer>(); 
     public ObservableCollection<Models.Customer> AllCustomers 
     { 
      get { return _customer; } 

     } 
     private Models.Customer _selectedItem; 
     public Models.Customer SelectedItem 
     { 
      get { return _selectedItem; } 

     } 
     public void LoadCustomers() 
     { 
      List<Models.Customer> list = DataAccessLayer.getcustomers(); 
      foreach (Models.Customer customer in list) 
      { 
       this._customer.Add(customer); 
      } 
      } 
     } 

和视图(在此刻后面没有代码):

<UserControl x:Class="Customers.Customer" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      xmlns:vm ="clr-namespace:Customers.ViewModels" 
      d:DesignHeight="300" d:DesignWidth="300" 
      xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit" > 

    <Grid> 
     <toolkit:DataGrid ItemsSource="{Binding AllCustomers}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" AutoGenerateColumns="True"> 

     </toolkit:DataGrid> 

     <toolkit:DataGrid ItemsSource="{Binding SelectedItem.Orders}"> 

     </toolkit:DataGrid> 



    </Grid> 
</UserControl> 

而且dataaccesslayer类:

class DataAccessLayer 
    { 
     public List<Customer> customers = new List<Customer>(); 

     public static List<Customer> getcustomers() 
     { 
      entities db = new entities(); 

      var customers = from c in db.Customer.Include("Orders") 
          select c; 
      return customers.ToList(); 
     } 


    } 

的问题是,没有数据仅仅因为没有设置数据上下文而显示。我试图在代码隐藏方面做到这一点,但没有奏效。无论如何,我宁愿在xaml文件中执行它。另一个问题是SelectedItem绑定 - 从不使用代码。

+0

在您的代码中,您的View Model上有AllCustomers属性,但您绑定了Customers。 – mak 2010-05-17 17:58:35

+0

我正在稍微清理代码并发出错字。工作代码中的绑定当然是正确的。 – EVA 2010-05-17 18:17:06

回答

1

this你在找什么?

+0

是和不是。我需要以某种方式调用方法getcustomers()。我应该在视图模型构造函数中执行它吗?看起来不太好。 – EVA 2010-05-17 17:57:56

+0

个人而言,我会在构造函数中调用getCustomers()的ViewModel。你觉得什么不好?当视图呈现时,它将创建一个ViewModel的新实例,并且此时您希望检索数据,所以构造函数可以做到这一点。 – 2010-05-17 18:07:42

+0

资源中的xaml构建如何? – EVA 2010-05-17 18:19:05

5

由于这是使用MVVM范例,所以我会在View的构造函数中实例化您的ViewModel。我查看/的ViewModels通常遵循这一系列事件:

  1. 查看被实例化
  2. 查看构造函数实例视图模型
  3. 视图模型初始化
  4. 视图模型运行数据获取程序(独立线程)
  5. 视图模型调用OnPropertyChanged (“”)来提醒View有什么改变;检查一切

我的视图模型从XAML代码隐藏实例化(抱歉,这是VB.NET,都没有得到解决,以学习C#以及足够用它相信自己):

Public Sub New() 
    MyBase.New() 
    Me.DataContext = New EditShipmentViewModel(Me) 'pass the view in to set as a View variable 
    Me.InitializeComponent() 
End Sub 

起初我希望有类似的东西

<UserControl> 
    <UserControl.DataContext> 
     <Local:EditShipmentViewModel> 
    </UserControl.DataContext> 
</UserControl> 

但这并不像我想要的那样。

希望有所帮助。

1

我以我观察Blend4的方式设置我的viewmodel datacontext。也就是说,如果我的视图模型被称为MainViewModel,我引用它的观点一样,:

<UserControl.Resources> 
    <local:MainViewModel x:Key="MainViewModelDataSource" /> 
</UserControl.Resources> 

<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource MainViewModelDataSource}}"> 
    ...view xaml stuff 

</Grid> 

还,如果你从你的视图模型的构造数据库加载数据时,不要忘了加帮助器方法如下:

if (!DesignerProperties.GetIsInDesignMode(new DependencyObject())) 
     { 
      myCollection = new ObservableCollection<Blah>(something); 
     } 

这样visual Studio/Blend4不会崩溃试图从设计器中的数据库连接检索数据。我个人经常在构造函数中加载数据,只是因为我马上需要它,并且从启动时将它缓存在内存中。

:)