IntelliSense for Data Binding not working
Asked Answered
E

4

31

After a couple of hours trying to debug an issue with data-binding that was caused by a mistyped property in a Binding extension. Once I noticed the mistake, the realization that if IntelliSense was available I may have not made the mistake in the first place. As a Visual Studio user who is used to error/warnings when mistyping a name; perhaps I'm spoiled, but the lack of IntelliSense led to the error.

I did some research and I found that Intellisense for Data Binding is available is Visual Studio 2013 which I'm using (Ultimate edition). I tried creating a simple WPF app following the second example in the blog. Firstly, There appears to be an error in the second example in the blog that resulted compiler error. Prefixing the Type=ViewModel:MainViewModel attribute with d: fixed the compiler error, yet the properties of my View-Model class are still not showing in the Intellisense menu. My code is below and in GitHub.

MainViewModel.cs:

using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace IntelliSenseForDataBinding
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            Greeting = "Hello World";
            Answer = 42;
        }

        private string _Greeting;
        public string Greeting
        {
            get { return _Greeting; }
            set { _Greeting = value; OnPropertyChanged(); }
        }

        private int _Answer;
        public int Answer
        {
            get { return _Answer; }
            set { _Answer = value; OnPropertyChanged(); }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}

MainWindow.xaml:

<Window x:Class="IntelliSenseForDataBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="450"
        d:DataContext="{d:DesignInstance Type=MainViewModel, IsDesignTimeCreatable=True}"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

    </Grid>
</Window>

MainWindows.xaml.cs:

using System.Windows;

namespace IntelliSenseForDataBinding
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            DataContext = new MainViewModel();
            InitializeComponent();
        }
    }
}

Here's the evidence that is not working:

enter image description here

I would expect to see an item for the 'Greeting' property in the IntelliSense menu. Any suggestions on why it's not there? I've also tried resetting the Visual Studio settings to default, just in case.

In addition, any suggestions on additional methods for preventing or detected mistyped property names in Binding attributes?

Extenuation answered 1/4, 2015 at 15:3 Comment(3)
I think this may be the answer: #24525914Extenuation
Nope: I've just checked and I do have Blend for Visual Studio installed. IntelliSense is working for everything else, just for Data Binding.Extenuation
in current version 16.7.2 I have the same issue again, accepted answer not work. Can anyone confirm? reported to ms developercommunity.visualstudio.com/content/problem/1155075/…Ebon
E
56

I opened your GitHub project in Visual Studio 2013 and I got the same behavior; no IntelliSense for bindings.

The design data is the key to the binding resolution which is failing, so I recommend this:

  1. Add your project namespace to your Window element: xmlns:local="clr-namespace:IntelliSenseForDataBinding" which can help resolve the location of VM.
  2. Change your d:DataContext to use the local namespace instead of d:Type, essentially providing the location of the type you're trying to use: d:DataContext="{d:DesignInstance local:MainViewModel, IsDesignTimeCreatable=True}"
  3. Clean, Build, and Test

Proof: enter image description here

Exclusive answered 1/4, 2015 at 17:25 Comment(3)
Thank you for this code, the binding not resolving at design time has always frustrated me! Follow up question, when I implement this the entire line d:DataContext..... is underlined, and the warning says "Value cannot be null. parameter name: context". There are no compile errors, and everything appears correctly, the IntelliSense also works in XAML now, it's just annoying to have that underlined.Wuhan
Note that the xmlns:mc and mc:Ingorable="d" are important for this to work too.Cresting
Thank you! This was driving me mad. Point 2 solved the issue.Nakitanalani
M
7

I know I am late, but Kcvin's answer really helped me a lot and I would like to add that some classes can also use DataType that helps IntelliSense do its magic.

example:

<DataTemplate x:Key="ItemTemplate" DataType="entities:Item">
        <Grid Height="60">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <TextBlock
                Grid.Column="0"
                Text="&#xE12B;"
                Style="{StaticResource MediumIconStyle}"
                Margin="{StaticResource XSmallLeftMargin}"
                AutomationProperties.Name="List item icon" />
            <StackPanel
                Grid.Column="1"
                Margin="{StaticResource SmallLeftMargin}"
                VerticalAlignment="Center">
                <TextBlock Style="{StaticResource ListTitleStyle}" Text="{Binding Name}" />
                <TextBlock Style="{StaticResource ListSubTitleStyle}" Text="{Binding Description}" />
            </StackPanel>
        </Grid>
    </DataTemplate>

I hope this helps someone in the future.

Madeira answered 4/4, 2020 at 19:11 Comment(0)
J
3

UPDATE: This issue has been resolved in the latest version of Visual Studio (v16.8 and higher). Simply upgrade to the latest version. Hopefully this issue stays fixed.

EDIT: The visual studio team is currently yet to provide a stable solution to this issue. According to this ticket a fix is available in Visual Studio 16.8 Preview 3. For the meantime, you can consider other creative workarounds present.

If none of the answers here worked for you, a possible way of troubleshooting this issue would be to use the Visual Studio Designer.

  1. Take your caret to a XAML element.

  2. Click on the Properties Tab (or simply press F4)

  3. Go to the DataContext property. If it appears to be empty, try pressing the New button. Property Tab with 'New' button

  4. If your designer gracefully crashes (like mine did), try to do more debugging from there.

Designer Crash

In my case the error looked like this: Could not resolve type 'System.Runtime.CompilerServices.TupleElementNamesAttribute' in assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

Which led me to this question and the installation of the System.Runtime nuget, and a few other changes.

Once the Properties tab is able to correctly identify your ViewModel Intellisense should start working property. If it doesn't, try restarting Visual Studio.

Jyoti answered 24/8, 2020 at 16:44 Comment(0)
D
2

Update Visual Studio to the latest versions, if you are using VS2019. This will resolve the issue.

Debatable answered 2/2, 2021 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.