拡張機能のプロパティの入力ミスが原因で発生したデータ バインディングの問題をデバッグしようと数時間試みた後、Binding
間違いに気付いたとき、IntelliSense が利用できれば最初から間違いを犯さなかったかもしれないと気づきました。名前の入力ミスによるエラーや警告に慣れている Visual Studio ユーザーとしては、甘やかされているだけかもしれませんが、IntelliSense がなかったためにエラーが発生していました。
調べてみたら、データバインディングのIntellisenseはVisual Studio 2013で利用可能です私が使用しているのは (Ultimate エディション) です。ブログの 2 番目の例に従って、簡単な WPF アプリを作成してみました。まず、ブログの 2 番目の例にはエラーがあり、コンパイラ エラーが発生したようです。Type=ViewModel:MainViewModel
属性の先頭にd:
コンパイラエラーを修正しましたが、View-ModelクラスのプロパティはまだIntellisenseメニューに表示されません。私のコードは以下のとおりです。GitHub。
メインビューモデル.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));
}
}
}
メインウィンドウ.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>
メインWindows.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 の設定をデフォルトにリセットすることも試しました。
さらに、バインディング属性でプロパティ名の入力ミスを防止または検出するための追加の方法についての提案はありますか?
ベストアンサー1
Visual Studio 2013 で GitHub プロジェクトを開いたところ、同じ動作が発生しました。バインディングに IntelliSense がありません。
設計データは、失敗しているバインディング解決の鍵となるため、次のことをお勧めします。
xmlns:local="clr-namespace:IntelliSenseForDataBinding"
プロジェクトの名前空間をWindow要素に追加します。解決するVM の場所。- を の代わりに 名前空間
d:DataContext
を使用するように変更し、基本的には使用しようとしている型の場所を指定します。local
d:Type
d:DataContext="{d:DesignInstance local:MainViewModel, IsDesignTimeCreatable=True}"
- クリーンアップ、ビルド、テスト
証拠: