wpf 用于数据绑定的 IntelliSense 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29394295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:03:40  来源:igfitidea点击:

IntelliSense for Data Binding not working

wpfdata-bindingvisual-studio-2013intellisense

提问by Dan Stevens

After a couple of hours trying to debug an issue with data-binding that was caused by a mistyped property in a Bindingextension. Once I noticed the mistake, the realization that if IntelliSense was available I may have not made the mistake in the first place. As a Visual Studio user who is used to error/warnings when mistyping a name; perhaps I'm spoiled, but the lack of IntelliSense led to the error.

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

I did some research and I found that Intellisense for Data Binding is available is Visual Studio 2013which I'm using (Ultimate edition). I tried creating a simple WPF app following the second example in the blog. Firstly, There appears to be an error in the second example in the blog that resulted compiler error. Prefixing the Type=ViewModel:MainViewModelattribute with d:fixed the compiler error, yet the properties of my View-Model class are still not showing in the Intellisense menu. My code is below and in GitHub.

我做了一些研究,我发现用于数据绑定的 Intellisense 可用是我正在使用的Visual Studio 2013(终极版)。我尝试按照博客中的第二个示例创建一个简单的 WPF 应用程序。首先,博客中的第二个示例中似乎有一个错误导致编译器错误。使用固定的编译器错误前缀Type=ViewModel:MainViewModel属性d:,但我的 View-Model 类的属性仍未显示在 Intellisense 菜单中。我的代码在下面和GitHub 中

MainViewModel.cs:

主视图模型.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:

主窗口.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:

主窗口.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();
        }
    }
}

Here's the evidence that is not working:

以下是无效的证据:

enter image description here

在此处输入图片说明

I would expect to see an item for the 'Greeting' property in the IntelliSense menu. Any suggestions on why it's not there? I've also tried resetting the Visual Studio settings to default, just in case.

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

In addition, any suggestions on additional methods for preventing or detected mistyped property names in Binding attributes?

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

回答by Kcvin

I opened your GitHub project in Visual Studio 2013 and I got the same behavior; no IntelliSense for bindings.

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

The design data is the key to the binding resolution which is failing, so I recommend this:

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

  1. Add your project namespace to your Window element: xmlns:local="clr-namespace:IntelliSenseForDataBinding"which can help resolvethe location of VM.
  2. Change your d:DataContextto use thelocalnamespace instead of d:Type, essentially providing the location of the type you're trying to use: d:DataContext="{d:DesignInstance local:MainViewModel, IsDesignTimeCreatable=True}"
  3. Clean, Build, and Test
  1. 将您的项目命名空间添加到您的 Window 元素:xmlns:local="clr-namespace:IntelliSenseForDataBinding"这可以帮助解析VM 的位置。
  2. 将您更改d:DataContext为使用local命名空间而不是d:Type,本质上提供您尝试使用的类型的位置:d:DataContext="{d:DesignInstance local:MainViewModel, IsDesignTimeCreatable=True}"
  3. 清理、构建和测试

Proof:enter image description here

证明:在此处输入图片说明

回答by Luk164

I know I am late, but Kcvin's answer really helped me a lot and I would like to add that some classes can also use DataTypethat helps IntelliSense do its magic.

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

example:

例子:

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

I hope this helps someone in the future.

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