Silverlight 3 と Prism に簡単なテスト アプリがあり、ビュー モデルで作成した簡単なコマンドにボタン Click をバインドしようとしています。これは、コマンドを機能させるためのテストアプリです。実行すると、ビューがコマンドを見つけられないことを示すバインド エラーが表示されます。
System.Windows.Data エラー: BindingExpression パス エラー: 'MyCommand' プロパティが 'Bind1.ShellViewModel' 'Bind1.ShellViewModel' に見つかりません (HashCode=8628710)。BindingExpression: Path='MyCommand' DataItem='Bind1.ShellViewModel' (HashCode=8628710); ターゲット要素は 'System.Windows.Controls.Button' (Name='') です。ターゲット プロパティは 'Command' (タイプ 'System.Windows.Input.ICommand') です。
ここに私のシェルビューがあります:
<UserControl
x:Class="Bind1.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Commands="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel>
<TextBlock Text="Hello World!"></TextBlock>
<Button Content="{Binding ButtonLabel}" Commands:Click.Command="{Binding Path=MyCommand}" />
</StackPanel>
</Grid>
</UserControl>
ビューのコンストラクターで、ViewModel をインスタンス化します (コンテナーの使用についてはまだ心配していません...)。
public partial class ShellView : UserControl
{
public ShellView()
{
InitializeComponent();
DataContext = new ShellViewModel();
}
}
ここに私のビューモデルがあります:
public class ShellViewModel
{
public string ButtonLabel { get { return "DoIt!!"; } }
public DelegateCommand<object> MyCommand = new DelegateCommand<object>(ExecuteMyCommand);
public static void ExecuteMyCommand(object obj)
{
Debug.WriteLine("Doit executed");
}
}
ボタン ラベル バインディングは正常に機能するため、ビューは ViewModel を正常に検出しています。
MyCommand が見つからないのはなぜですか? それは私を怒らせています-私は明らかに単純な非常に間違ったことをしています...
どうもありがとう。