1

在我的表示层 (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); }
    }

现在在我的视图层(PrintPage.xaml.cs)中,我正在以编程方式创建一个数据网格,完成后我需要设置 itemsource,如下所示:

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

但是,这会产生以下错误:
非静态字段、方法或属性“Presentation.Print.PrintViewModel.ViewFullRecipeGrouping.get”需要对象引用

我知道我的数据集没问题,因为如果我直接在 XAML 中设置它,它工作得非常好(对于我在 XAML 本身中创建的测试数据网格)。

所以,我想问题在于我如何从 PRESENTATION 层(在我的 VIEW 层)访问 ViewFullRecipeGrouping。

这是 PrintViewModel 的实例化方式:

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

关于如何完成这项工作或更好的方法的任何想法或建议(我在这里违反了 MVVM 吗?)谢谢,

4

2 回答 2

1

我相信您收到此错误的原因:

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

是因为您试图访问property它,就好像它是静态的一样。在不了解您的 ViewModel 类的情况下,该类本身是静态的吗?如果不是,则您尝试访问它的方式将不起作用..您需要先实例化该类,然后像这样访问该属性:(您可能还需要设置数据上下文)

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

我还想问一下,您为什么要以编程方式创建 DataGrid?为什么不通过 XAML 定义它并为 ItemsSource 使用 DataBinding。

另外,我想指出 Properties 的意义在于封装。您正在为公共成员变量使用“getter”。成员变量实际上应该是私有的:

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

编辑: 好的,因为您使用“工厂”来获取看起来像 ViewModel 的单例实例,请将代码更新为:

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

或尝试设置 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);
  }
于 2013-04-26T05:05:07.297 回答
0

在没有看到您的其余代码的情况下,我只能猜测,但该分配代码似乎包含在静态方法中。

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

如果是这种情况,您将需要将赋值方法设为非静态(很可能是最简单的修复),将您的ViewFullRecipeGrouping属性放在它自己的静态方法中,或者在赋值代码的上下文中创建属性的实例, IE。

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;
}
于 2013-04-26T03:56:32.857 回答