簡體   English   中英

從PRESENTATION層(MVVM)訪問VIEW中的數據以獲取datagrid項源

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

在我的表示層(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?)謝謝,

我相信您收到此錯誤的原因:

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

是因為您試圖像訪問靜態property一樣訪問該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定義它,並將DataBinding用於ItemsSource。

另外,我想指出,屬性的意義在於封裝。 您正在為公共成員變量使用“獲取程序”。成員變量實際上應該是私有的:

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

編輯:好的,因為您使用的是“工廠”來獲取看起來像是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;
  }

或嘗試設置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);
  }

沒有看到其余的代碼,我只能猜測,但是看來此分配代碼包含在靜態方法中。

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屬性置於其自己的靜態方法中,或者在該賦值上下文中創建該屬性的實例。代碼,即

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