Seeing ViewModel Properties when Binding

Feb 16, 2014 at 5:58 PM
This is for the new Prism 4.2, but not an item to selected below.

I noticed there were some new features added in VS 2013 for XAML and binding. One was to be able to see the properties of item that was bound to the UI component. As an example, if I had a Person Class bound to a Grid with TextBlocks, I should be able to see properties (e.g. FirstName, LastName, etc.) in the XAML when typing <TextBlock Text={Binding ...

I thought with the new locator and features within would work with the new bits. It still works and will be displayed in the UI. I'm I missing something?

Thanks
Feb 17, 2014 at 5:48 PM
Hi shaggygi,

I believe you wanted to say that Binding IntelliSense is still not working for you. Based on my understanding, there are a few things that need to be declared in order to make IntelliSense work:

  • Data Context should be defined in View's Xaml code without being injected from the container. So it would be available for DesignTime and Binding can resolve the Properties with IntelliSense. Local namespace declaration would be needed to reference the DataContext.
  • Also, you would need to declare the following namespaces for the proper IntelliSense to work:
   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   mc:Ignorable="d"
Bear in mind that declaring the DataContext directly into xaml code couples the View with its ViewModel/Model and the container is not able to resolve any dependency from it. Therefore, one possible way to solve this would be to declare a Mock ViewModel which implements every property into xaml code, and resolve the proper ViewModel from the container.

I hope this helped you,
Regards.

Gabriel Ostrowsky.
https://blogs.southworks.net/gostrowsky
Feb 18, 2014 at 12:47 AM
Yes, that is exactly what I was trying to explain. Thanks for the help.
Feb 18, 2014 at 3:50 PM
We make a property in the view that is the same type of the viewmodel that will be injected in and set that property as the datacontext in xaml. We do tie our views to the viewmodel but not the viewmodel to the view. Then we constructor inject the same type and store it to the property.

Example: of the code behind
public partial class DashboardView
{
        public DashboardViewModel ViewModel { get; set; }

        // need empty constructor for the design time but unity will inject into the other constructor
        public DashboardView()
        {
            InitializeComponent();
        }

        // At run time unity will use this constructor
        public DashboardView(DashboardViewModel viewModel)
        {
            ViewModel = viewModel;
            InitializeComponent();
        }
}
Example of the Xaml
<UserControl x:Class="DashboardView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      DataContext="{Binding RelativeSource={RelativeSource Self}, Path=ViewModel}">
I haven't tried this in VS 2013 but we use Resharper in VS 2012 and it picks up the datacontext this way. I would hope it works in VS 2013.