Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliSense for Data Binding not working

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?

like image 376
Dan Stevens Avatar asked Apr 01 '15 15:04

Dan Stevens


4 Answers

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

like image 108
Kcvin Avatar answered Nov 19 '22 04:11

Kcvin


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.

like image 7
Luk164 Avatar answered Nov 19 '22 04:11

Luk164


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.

like image 3
Prince Owen Avatar answered Nov 19 '22 04:11

Prince Owen


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

like image 2
Basharat Hussain Avatar answered Nov 19 '22 05:11

Basharat Hussain