简体   繁体   English

从PRESENTATION层(MVVM)访问VIEW中的数据以获取datagrid项源

[英]Accessing data in VIEW from PRESENTATION layer (MVVM) for datagrid itemsource

In my presentation layer (PrintViewModel.cs) I have the following code where I expose my dataset that will be used to populate a datagrid. 在我的表示层(PrintViewModel.cs)中,我具有以下代码,其中暴露了将用于填充数据网格的数据集。

    public const string ViewFullRecipeGroupingPropertyName = "ViewFullRecipeGrouping";
    public List<ViewFullRecipe> _viewFullRecipeGrouping = new List<ViewFullRecipe>();
    public List<ViewFullRecipe> ViewFullRecipeGrouping
    {
        get { return _viewFullRecipeGrouping; }
        set { Set(ViewFullRecipeGroupingPropertyName, ref _viewFullRecipeGrouping, value, true); }
    }

Now in my view layer (PrintPage.xaml.cs) I am creating a datagrid programatically and when done I need to set the itemsource, something like this: 现在在我的视图层(PrintPage.xaml.cs)中,我正在以编程方式创建一个数据网格,完成后,我需要设置itemsource,如下所示:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
    var datagrid = new DataGrid();
    datagrid.ItemsSource = PrintViewModel.ViewFullRecipeGrouping;
    }

However this is generating the following error: 但是,这会产生以下错误:
An object reference is required for the non-static field, method, or property 'Presentation.Print.PrintViewModel.ViewFullRecipeGrouping.get' 非静态字段,方法或属性“ Presentation.Print.PrintViewModel.ViewFullRecipeGrouping.get”需要对象引用。

I know my dataset is ok because if I set it directly in the XAML it works perfectly fine (for my test datagrid created in the XAML itself). 我知道我的数据集是可以的,因为如果直接在XAML中设置它,就可以很好地工作(对于在XAML本身中创建的测试数据网格)。

So, I guess the issue is on HOW I am accessing ViewFullRecipeGrouping from the PRESENTATION layer (in my VIEW layer). 因此,我想问题出在我如何从PRESENTATION层(在我的VIEW层中)访问ViewFullRecipeGrouping。

This is how the PrintViewModel is instantiated: 这是实例化PrintViewModel的方式:

public class ViewModelLocator
{
    static ViewModelLocator()
    {
        SimpleIoc.Default.Register<PrintViewModel>(true);
    }
    public PrintViewModel Print
    {
        get
        {
            return ServiceLocator.Current.GetInstance<PrintViewModel>();
        }
    }

Any ideas or suggestions on either how to get this work or a better way (am I violating MVVM here?) Thanks, 关于如何获得此工作或更好的方法的任何想法或建议(我是否违反了MVVM?)谢谢,

I believe the reason why you are getting this error: 我相信您收到此错误的原因:

An object reference is required for the non-static field, method, or property 'Presentation.Print.PrintViewModel.ViewFullRecipeGrouping.get'

is because you're trying to access the property as if it is static. 是因为您试图像访问静态property一样访问该property Without knowing too much about your ViewModel class, is the class static itself? 在不了解您的ViewModel类的情况下,该类本身是静态的吗? If it's not, the way you are trying to access it won't work.. you will need to first instantiate the class, then access the property like so: (you may also need to set the data context) 如果不是,则您尝试访问它的方式将不起作用..您将需要首先实例化该类,然后按如下方式访问属性:(您可能还需要设置数据上下文)

  private PrintViewModel _viewModel = new PrintViewModel();
  private void Window_Loaded(object sender, RoutedEventArgs e)
  {
    this.DataContext = _viewModel;
    var datagrid = new DataGrid();
    datagrid.ItemsSource = _viewModel.ViewFullRecipeGrouping;
  }

I would also like to ask, why are you programmatically creating the DataGrid? 我还想问一下,为什么要以编程方式创建DataGrid? Why not define it via XAML and and use DataBinding for the ItemsSource. 为什么不通过XAML定义它,并将DataBinding用于ItemsSource。

Also, I'd like to note that the point of Properties is for encapsulation. 另外,我想指出,属性的意义在于封装。 You are using a "getter" for a public member variable.. The member variable should actually be private: 您正在为公共成员变量使用“获取程序”。成员变量实际上应该是私有的:

private List<ViewFullRecipe> _viewFullRecipeGrouping = new List<ViewFullRecipe>();
public List<ViewFullRecipe> ViewFullRecipeGrouping
{
    get { return _viewFullRecipeGrouping; }
    set { Set(ViewFullRecipeGroupingPropertyName, ref _viewFullRecipeGrouping, value, true); }
}

Edit: Okay, since you are using a "factory" to get what looks like a singelton instance of the ViewModel, update the code to: 编辑:好的,因为您使用的是“工厂”来获取看起来像是ViewModel的singelton实例的东西,所以将代码更新为:

  private ViewModelLocator _locator = new ViewModelLocator();
  private void Window_Loaded(object sender, RoutedEventArgs e)
  {
    var viewModel = _locator.Print; // your ViewModel instance
    var datagrid = new DataGrid();
    datagrid.ItemsSource = viewModel.ViewFullRecipeGrouping;
  }

or try setting the DataBinding of the GridView 或尝试设置GridView的DataBinding

  private ViewModelLocator _locator = new ViewModelLocator();
  private void Window_Loaded(object sender, RoutedEventArgs e)
  {
    var viewModel = _locator.Print; // your ViewModel instance
    this.DataContext = viewModel;

    var datagrid = new DataGrid();
    var binding = new Binding("ViewFullRecipeGrouping");
    BindingOperations.SetBinding(datagrid, DataGrid.ItemsSource, binding);
  }

Without seeing the rest of your code I can only guess, but it appears that this assignment code is contained in a static method. 没有看到其余的代码,我只能猜测,但是看来此分配代码包含在静态方法中。

public static void AssignSource() // I'm guessing at the name here.
{
    var datagrid = new DataGrid();
    //ViewFullRecipeGrouping is not static so will throw the exception.
    datagrid.ItemsSource = PrintViewModel.ViewFullRecipeGrouping;
}

If that is the case, you will need to either make the assignment method non-static (most likely easiest fix), put your ViewFullRecipeGrouping property in its own static method, or create an instance of the of the property within the context of the assignment code, ie. 在这种情况下,您将需要使分配方法成为非静态方法(最有可能是最简单的解决方法),将ViewFullRecipeGrouping属性置于其自己的静态方法中,或者在该赋值上下文中创建该属性的实例。代码,即

public static void AssignSource() // I'm guessing at the name here.
{
    PrintViewModel.ViewFullRecipeGrouping = new List<ViewFullRecipe>();
    PrintViewModel.ViewFullRecipeGrouping.Add(new ViewFullRecipe());
    var datagrid = new DataGrid();
    datagrid.ItemsSource = PrintViewModel.ViewFullRecipeGrouping;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM