私はViewModelにバインドしたい単純なビューを持っています。私は現在、データバインディングにSource =形式を使用していますが、コード内のDataContextを指定するために変換したいと考えています。コード内にDataContextを指定
これは私が持っているものであり、それは働いている...
XAML:
<Window.Resources>
<local:ViewModel x:Key="ViewModel" />
</Window.Resources>
<Button Content="Click">
<local:EventToCommand.Collection>
<local:EventToCommandCollection>
<local:EventToCommand Event="Click" Command="{Binding Source={StaticResource ViewModel}, Path=ClickCommand, diag:PresentationTraceSources.TraceLevel=High}" />
<local:EventToCommand Event="GotFocus" Command="{Binding Source={StaticResource ViewModel}, Path=GotFocusCommand}" />
</local:EventToCommandCollection>
</local:EventToCommand.Collection>
</Button>
</Window>
のViewModelコード:
public class ViewModel
{
public Command ClickCommand { get; set; }
public Command GotFocusCommand { get; set; }
public ViewModel()
{
ClickCommand = new Command((obj) => { Execute(obj); return null; });
GotFocusCommand = new Command((obj) => { Execute(obj); return null; });
}
void Execute(object param)
{
if (param != null)
System.Windows.MessageBox.Show(param.ToString());
else
System.Windows.MessageBox.Show("Execute");
}
}
今、私がやりたいすべては私の窓の中でこれですコードの後ろ:
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
を削除し、XAMLのWindow.Resourcesセクションを削除しますが、バインディング文字列をどのように変更する必要があるかはわかりません。
あなたは「あなたがわからない」とはどういう意味ですか?あなたはXAMLコードにアクセスできませんか? XAMLで使用したすべてのバインディングエフェクトから 'Source = {StaticResource ViewModel}'セクションを削除することができますか?これを行うと、データは 'DataContext'自体から取得されます。 –
下記の私のコメントを参照してください... – Oliver
@Olivger、http://adammills.wordpress.com/2011/02/14/eventtocommand-action-mvvm-glue/によると、@レイチェルはうまくいくはずです。あなたのビジュアルスタジオの 'Output'ウィンドウを見て、バインディングエラーがあるかどうかを確認してください ... –