WPF data context for design time and run time

后端 未结 2 1795
南方客
南方客 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: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:

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

    Hope this helps...

提交回复
热议问题