wpf - 用于数据绑定(bind)的 IntelliSense 不起作用

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

在尝试调试由 Binding 中的错误类型属性引起的数据绑定(bind)问题几个小时后延期。一旦我注意到这个错误,我就会意识到如果 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();
        }
    }
}

以下是无效的证据:

enter image description here

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

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

最佳答案

我在 Visual Studio 2013 中打开了你的 GitHub 项目,我得到了同样的行为;没有用于绑定(bind)的 IntelliSense。
设计数据是绑定(bind)解析失败的关键,所以我推荐这个:

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

  • 证明:
    enter image description here

    关于wpf - 用于数据绑定(bind)的 IntelliSense 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29394295/

    相关文章:

    c# - WPF DataGrid 不退出编辑模式

    c# - 使用面板创建动态网格

    javascript - 数据绑定(bind)到 Mustache 模板? (最好干净简单)

    visual-studio - 在 Visual Studio 2013 中禁用设置的云同步?

    c# - 对不一致的行结尾说"is"?

    c# - WPF:控件失去其绑定(bind)

    c# - 如何根据条件在 wpf datagrid 中显示以下输出

    c# - 通过绑定(bind)部分填充 MenuItem

    javascript - 如何正确绑定(bind)CoreUI Switch?

    visual-studio-2013 - 如何在评论中禁用 Visual Studio 的自动完成