【发布时间】: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 设置重置为默认值,以防万一。
此外,对于防止或检测绑定属性中错误输入的属性名称的其他方法有什么建议吗?
【问题讨论】:
-
不:我刚刚检查过,我确实安装了 Blend for Visual Studio。 IntelliSense 适用于其他一切,仅适用于数据绑定。
-
在当前版本 16.7.2 我又遇到了同样的问题,接受的答案不起作用。任何人都可以确认吗?报告给女士developercommunity.visualstudio.com/content/problem/1155075/…
标签: wpf data-binding visual-studio-2013 intellisense