IntelliSense for Data Binding无法正常工作

Dan*_*ens 14 data-binding wpf intellisense visual-studio-2013

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

我做了一些研究,我发现可以使用Intellisense for Data Binding是我正在使用的Visual Studio 2013(终极版).我尝试按照博客中的第二个示例创建一个简单的WPF应用程序.首先,博客中的第二个示例中出现了导致编译器错误的错误.Type=ViewModel:MainViewModeld:固定编译器错误的情况下属性进行前缀,但我的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));
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是不起作用的证据:

在此输入图像描述

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

此外,有关在Binding属性中防止或检测错误的属性名称的其他方法的任何建议?

Kcv*_*vin 35

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

设计数据是失败的绑定解析的关键,所以我建议这样做:

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

证明: 在此输入图像描述

  • 感谢您提供这段代码,在设计时无法解析的绑定总是让我感到沮丧!后续问题,当我实现此操作时,整行 d:DataContext..... 都带有下划线,并且警告显示“值不能为空。参数名称:上下文”。没有编译错误,一切都显示正确,IntelliSense 现在也可以在 XAML 中工作,只是有下划线很烦人。 (3认同)

Luk*_*164 6

我知道我迟到了,但 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>
Run Code Online (Sandbox Code Playgroud)

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