亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

綁定數據網格按鈕 mvvm

綁定數據網格按鈕 mvvm

C#
慕勒3428872 2023-08-20 15:40:47
我有數據網格,其中一列是按鈕列,我想將此單擊綁定到視圖模型,但它沒有到達視圖模型功能。<DataGrid  CanUserAddRows="False" AutoGenerateColumns="False"  ItemsSource="{Binding TableComments}" SelectedItem="{Binding SelectedRow}"     x:Name="dataGrid" >    <DataGrid.Columns >                           <DataGridTemplateColumn Header="Delete">            <DataGridTemplateColumn.CellTemplate>                 <DataTemplate>                    <Button Command="{Binding DeleteCommentCommand}" >Delete</Button>                 </DataTemplate>             </DataGridTemplateColumn.CellTemplate>         </DataGridTemplateColumn>   </DataGrid.Columns></DataGrid>在視圖模型中:public ICommand DeleteCommentCommand { get; private set; }public MyViewModel(){   DeleteCommentCommand = new RelayCommand(Delete);}void Delete(){}我感覺問題出在這一行: <Button Command="{Binding DeleteCommentCommand}" >Delete</Button>
查看完整描述

1 回答

?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

DeleteCommentCommand綁定到 DataGrid item DataContext,而不是 DataGridDataContext本身(ViewModel)。您應該設置適當的 DataContextCellTemplate或更新一點綁定,例如


Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.DeleteCommentCommand}"

您還可以更新命令并將參數傳遞給RelayCommand方法Delete


public MyViewModel()

{

   DeleteCommentCommand = new RelayCommand(item => Delete(item));

}


void Delete(object item)

{

}

并在xaml中傳遞值CommandParameter="{Binding}"


查看完整回答
反對 回復 2023-08-20
  • 1 回答
  • 0 關注
  • 137 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號