WPF data context for design time and run time

后端 未结 2 1793
南方客
南方客 2021-02-06 18:33

I\'m learning WPF, MVVM Light and the ViewModelLocator pattern and running into difficulties with my main window\'s data context.

public class ViewModelLocator
          


        
相关标签:
2条回答
  • 2021-02-06 18:44

    The last line will work nicely if you remove the comma after d:DesignInstance:

    d:DataContext="{d:DesignInstance Type=viewModels:MainViewModel,
    IsDesignTimeCreatable=True}">
    
    0 讨论(0)
  • 2021-02-06 18:53

    Using the default data context should also work in design time:

    DataContext="{Binding Main, Source={StaticResource Locator}}"
    

    If not, try to compile the proyect and check out again. You can manage the properties values that you want to show in design time by using the IsInDesignMode property that the MvvmLight Toolkit provides. By default the MainViewModel's constructor looks like this:

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
            }
            else
            {
                // Code runs "for real"
            }
        }
    

    Hope this helps...

    0 讨论(0)
提交回复
热议问题