Visual Studio Intellisense doesn't suggest source-generated properties when binding in WPF

Viewed 209

I'm not getting intellisense suggestions for properties that were source generated by the MVVM community toolkit. Is there a way to fix this? If I can't get the suggestions to work, then using [ObservableProperty] isn't even worth it IMO. Example below.

ViewModel has one source generated property and one handmade property:

public partial class SimplisticViewModel : ObservableObject
{
    [ObservableProperty]
    private int sourceGeneratedProperty;

    private float handmadeProperty;
    public float HandmadeProperty
    {
        get => handmadeProperty;
        set => SetProperty(ref handmadeProperty, value);
    }
}

XAML:

<Window x:Class="WPFEFTest.TestWindow"
        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"
        xmlns:local="clr-namespace:WPFEFTest"
        mc:Ignorable="d"
        Title="TestWindow" Height="450" Width="800">

    <Window.DataContext>
        <local:SimplisticViewModel/>
    </Window.DataContext>
    
    <StackPanel>
        <TextBox Text=""/>
    </StackPanel>
</Window>

When setting up binding for the handmade property, the suggestions work:

handmade property binding

When setting up binding for the source generated property, there are no suggestions:

source generated property binding

However, if I write the entire property name (avoiding typos and such...) and hover my cursor over it (cursor not visible in screenshot), intellisense will correctly show the type of the property. So the problem probably isn't that intellisense doesn't "see" the property at all.

cursor hover over source generated property

EDIT: To make it clear I'm not trying to bind to a field, below is code that MVVM community toolkit generates because of the ObservableProperty attribute:

public partial class SimplisticViewModel
{
    [global::System.CodeDom.Compiler.GeneratedCode("Microsoft.Toolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "7.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCode]
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    public int SourceGeneratedProperty
    {
        get => sourceGeneratedProperty;
        set
        {
            if (!global::System.Collections.Generic.EqualityComparer<int>.Default.Equals(sourceGeneratedProperty, value))
            {
                OnPropertyChanging(global::Microsoft.Toolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.SourceGeneratedPropertyPropertyChangingEventArgs);
                sourceGeneratedProperty = value;
                OnPropertyChanged(global::Microsoft.Toolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.SourceGeneratedPropertyPropertyChangedEventArgs);
            }
        }
    }
}
1 Answers

Maybe you could try setting DataContext for window... Same thing/issue you describe happens when I remove d:DataContext which points to ViewModel class and when DataContext is set, Intellisense picks up properties from ViewModel.

<Window x:Class="WPFEFTest.TestWindow"
    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"
    xmlns:local="clr-namespace:WPFEFTest"
    xmlns:viewmodels="clr-namespace:WPFEFTest.ViewModels"
    mc:Ignorable="d"
    Title="TestWindow" Height="450" Width="800" d:DataContext="{d:DesignInstance Type=viewmodels:SimplisticViewModel>
Related