2 回答

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個(gè)贊
你沒有顯示你所有的代碼和上下文,但它應(yīng)該像這樣工作我假設(shè)你在一個(gè)用戶控件中校準(zhǔn)父數(shù)據(jù)上下文......(以列表視圖為例):
<ListView ItemsSource="{Binding listFromDataContext, IsAsync=True}" Margin="3,51,0,10" >
<ListView.ItemTemplate >
<DataTemplate>
<grid>
<Button Command="{Binding DataContext.MyMethode, RelativeSource={RelativeSource AncestorType={x:Type controls:thisUserControl}}}" CommandParameter="{Binding}" />
</grid>
</DataTemplate>
</ListView.ItemTemplate >
</ListView>
然后在模型中
private ICommand _MyMethode;
public ICommand MyMethode
{
get
{
return _MyMethode ?? (_MyMethode = new CommandHandler<MyModel.item>(x => showMessage(x), _canExecute));
}
}
public void showMessage(MyModel.item x)
{
MessageBox.Show(x.Info);
}

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個(gè)贊
首先,如果您Button
在 a 中Grid
,DataContext
則不需要設(shè)置Path
like Path=ParentRow.DataContext
。
你的Command
綁定應(yīng)該是這樣的:
<Button Command="{Binding YourVMICommand"} />
您必須public ICommand
在 VM 中定義一個(gè),然后將其綁定到按鈕。
- 2 回答
- 0 關(guān)注
- 179 瀏覽
添加回答
舉報(bào)