【问题标题】:IntelliSense for Data Binding not working用于数据绑定的 IntelliSense 不起作用
【发布时间】:2015-06-06 07:21:52
【问题描述】:

经过几个小时尝试调试由 Binding 扩展中的错误类型属性引起的数据绑定问题。一旦我注意到这个错误,我就会意识到如果 IntelliSense 可用,我可能一开始就没有犯错。作为一个习惯于在错误输入名称时出现错误/警告的 Visual Studio 用户;也许我被宠坏了,但是缺少 IntelliSense 导致了错误。

我做了一些研究,发现我正在使用Intellisense for Data Binding is available is Visual Studio 2013(终极版)。我尝试按照博客中的第二个示例创建一个简单的 WPF 应用程序。首先,博客中的第二个示例中似乎存在错误,导致编译器错误。 Prefixing the Type=ViewModel:MainViewModel attribute with d: 修复了编译器错误,但我的 View-Model 类的属性仍未显示在 Intellisense 菜单中。我的代码在下面和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();
        }
    }
}

以下是无效的证据:

我希望在 IntelliSense 菜单中看到“Greeting”属性项。关于为什么它不存在的任何建议?我还尝试将 Visual Studio 设置重置为默认值,以防万一。

此外,对于防止或检测绑定属性中错误输入的属性名称的其他方法有什么建议吗?

【问题讨论】:

标签: wpf data-binding visual-studio-2013 intellisense


【解决方案1】:

我在 Visual Studio 2013 中打开了你的 GitHub 项目,我得到了同样的行为;没有用于绑定的 IntelliSense。

设计数据是绑定解析失败的关键,所以我推荐这个:

  1. 将您的项目命名空间添加到您的 Window 元素:xmlns:local="clr-namespace:IntelliSenseForDataBinding" 这可以帮助解析虚拟机的位置。
  2. 更改您的d:DataContext 以使用local 命名空间而不是d:Type,本质上提供您尝试使用的类型的位置:d:DataContext="{d:DesignInstance local:MainViewModel, IsDesignTimeCreatable=True}"
  3. 清理、构建和测试

证明:

【讨论】:

  • 感谢您提供此代码,在设计时未解决的绑定一直让我感到沮丧!跟进问题,当我实现这一点时,整行 d:DataContext..... 都带有下划线,并且警告说“值不能为空。参数名称:上下文”。没有编译错误,一切都显示正确,IntelliSense 现在也可以在 XAML 中工作,只是下划线很烦人。
  • 请注意,xmlns:mcmc:Ingorable="d" 对此也很重要。
【解决方案2】:

我知道我迟到了,但 Kcvin 的回答确实对我帮助很大,我想补充一点,有些课程也可以使用 DataType,这有助于 IntelliSense 发挥它的魔力。

示例:

<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>

我希望这对将来的某人有所帮助。

【讨论】:

    【解决方案3】:

    更新: 此问题已在最新版本的 Visual Studio(v16.8 及更高版本)中得到解决。只需upgrade to the latest version. 希望这个问题得到解决。

    编辑: Visual Studio 团队目前尚未为此问题提供稳定的解决方案。根据这个ticketVisual Studio 16.8 Preview 3 中提供了一个修复程序。同时,你可以考虑other creative workarounds出席。

    如果此处的任何答案都不适合您,解决此问题的一种可能方法是使用 Visual Studio 设计器。

    1. 将插入符号添加到 XAML 元素。

    2. 单击“属性”选项卡(或直接按 F4)

    3. 转到 DataContext 属性。如果它看起来是空的,请尝试按下New 按钮。

    4. 如果您的设计器正常崩溃(就像我的那样),请尝试从那里进行更多调试。

    在我的情况下,错误如下所示:无法在程序集“mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中解析类型“System.Runtime.CompilerServices.TupleElementNamesAttribute”。

    这让我想到了这个questionSystem.Runtime nuget 的安装,以及其他一些更改。

    一旦Properties 选项卡能够正确识别您的ViewModel,Intellisense 应该开始工作属性。如果没有,请尝试重新启动 Visual Studio。

    【讨论】:

      【解决方案4】:

      如果您使用的是 VS2019,请将 Visual Studio 更新到最新版本。 这将解决问题。

      【讨论】:

        猜你喜欢
        • 2015-06-07
        • 1970-01-01
        • 2014-12-29
        • 2013-06-03
        • 1970-01-01
        • 1970-01-01
        • 2012-09-02
        • 2016-01-16
        相关资源
        最近更新 更多