2016-02-12 2 views
0

Есть ли способ передать ребенку весь класс родительского контроля коллекции (в моем случае ListBox) в Command в ViewModel? Я могу передать любое значение «IsChecked», «AttachmentId» или «Name». Все три свойства являются частью класса «Присоединение». «AttachmentLst» (элемент источника ListBox содержит элементы «Attachment». Я хотел бы передать весь класс в свою команду. Ниже приведен рабочий код для передачи только одного свойства, в данном случае «AttachmentId». Как передать «Приложение» в Команда?Передача элемента (всего класса) списка родительского контроля в команду

  <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>

ViewModel

 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 Binding. DataContext будет одинаковым для всех.

+1

приятно, спасибо – lucas

+0

@ mauro21pl ваш прием. –

Смежные вопросы