2016-02-12 21 views
0

有沒有辦法將子類的父級集合控制(在我的情況下是ListBox)的整個類傳遞給ViewModel中的命令?我可以傳遞任何單個值,「IsChecked」,「AttachmentId」或「Name」。所有這三個屬性都是「附件」類的一部分。 'AttachmentLst'(ListBox的項目源代碼包含'Attachment'元素,我想將整個類傳遞給我的Command,下面的代碼只是傳遞一個屬性,在這種情況下'AttachmentId'如何傳遞'Attachment'到命令?將父級控制列表的項目(整個班級)傳遞給命令

  <telerik:RadListBox telerik:StyleManager.Theme="Windows8" Name="lstAttachments" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" ItemsSource="{Binding AttachmentLst}"> 
 
       <telerik:RadListBox.ItemTemplate> 
 
        <DataTemplate> 
 
         <StackPanel Orientation="Horizontal"> 
 
          <CheckBox IsChecked="{Binding IsChecked}" Command="{Binding ElementName=lstAttachments, Path=DataContext.AttachmentChecked}" CommandParameter="{Binding AttachmentId}" Content="Test" /> 
 
          <Label Content="{Binding AttachmentId}" FontSize="12"></Label> 
 
          <Label Content="{Binding Name}" FontSize="12"></Label> 
 
         </StackPanel> 
 
        </DataTemplate> 
 
       </telerik:RadListBox.ItemTemplate> 
 
      </telerik:RadListBox>

視圖模型

 public ObservableCollection<Attachment> AttachmentLst 
 
     { 
 
      get { return _attachmentLst; } 
 
      set { SetProperty(ref _attachmentLst, value); } 
 
     } 
 
     #endregion 
 

 
     #region Commands 
 
     public ICommand AttachmentChecked 
 
     { 
 
      get 
 
      { 
 
       return _attachmentChecked ?? (_attachmentChecked = new CommandHandlerWithParam(obj => ExecuteAttachmentChecked(obj), CanExecuteAttachmentChecked())); 
 
      } 
 
     } 
 

 
     private void ExecuteAttachmentChecked(object obj) 
 
     { 
 

 
     } 
 

 
     private bool CanExecuteAttachmentChecked() 
 
     { 
 
      return true; 
 
     }

回答

0

使用

CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}},Path=DataContext}" 

通過整個項目作爲CommandParameter

您還可以使用templateParent/Self結合,而不是Ancestor 約束力。 DataContext對所有人都是一樣的。

+1

不錯,非常感謝 – lucas

+0

@ mauro21pl您的歡迎。 –

相關問題