Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing data in VIEW from PRESENTATION layer (MVVM) for datagrid itemsource
    primarykey
    data
    text
    <p>In my presentation layer (PrintViewModel.cs) I have the following code where I expose my dataset that will be used to populate a datagrid.</p> <pre><code> public const string ViewFullRecipeGroupingPropertyName = "ViewFullRecipeGrouping"; public List&lt;ViewFullRecipe&gt; _viewFullRecipeGrouping = new List&lt;ViewFullRecipe&gt;(); public List&lt;ViewFullRecipe&gt; ViewFullRecipeGrouping { get { return _viewFullRecipeGrouping; } set { Set(ViewFullRecipeGroupingPropertyName, ref _viewFullRecipeGrouping, value, true); } } </code></pre> <p>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:</p> <pre><code> private void Window_Loaded(object sender, RoutedEventArgs e) { var datagrid = new DataGrid(); datagrid.ItemsSource = PrintViewModel.ViewFullRecipeGrouping; } </code></pre> <p>However this is generating the following error:<br> An object reference is required for the non-static field, method, or property 'Presentation.Print.PrintViewModel.ViewFullRecipeGrouping.get'</p> <p>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).</p> <p>So, I guess the issue is on HOW I am accessing ViewFullRecipeGrouping from the PRESENTATION layer (in my VIEW layer).</p> <p>This is how the PrintViewModel is instantiated:</p> <pre><code>public class ViewModelLocator { static ViewModelLocator() { SimpleIoc.Default.Register&lt;PrintViewModel&gt;(true); } public PrintViewModel Print { get { return ServiceLocator.Current.GetInstance&lt;PrintViewModel&gt;(); } } </code></pre> <p>Any ideas or suggestions on either how to get this work or a better way (am I violating MVVM here?) Thanks,</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload