在尝试调试由 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();
}
}
}
以下是无效的证据:
我希望在 IntelliSense 菜单中看到“Greeting”属性的项目。关于为什么它不存在的任何建议?我还尝试将 Visual Studio 设置重置为默认值,以防万一。
此外,关于防止或检测绑定(bind)属性中错误输入的属性名称的其他方法有什么建议吗?
最佳答案
我在 Visual Studio 2013 中打开了你的 GitHub 项目,我得到了同样的行为;没有用于绑定(bind)的 IntelliSense。
设计数据是绑定(bind)解析失败的关键,所以我推荐这个:
xmlns:local="clr-namespace:IntelliSenseForDataBinding"
哪位有帮助解决 虚拟机的位置。 d:DataContext
使用 local
命名空间而不是 d:Type
,本质上是提供您尝试使用的类型的位置:d:DataContext="{d:DesignInstance local:MainViewModel, IsDesignTimeCreatable=True}"
证明:
关于wpf - 用于数据绑定(bind)的 IntelliSense 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29394295/