2017-04-26 14 views
1

관찰 가능한 객체 컬렉션을 전달하고 버튼으로 요소를 표시하는 항목 컨트롤이 있습니다. DelegateCommands를 사용하여 View Model에서 버튼 클릭을 캡처하고 있습니다.버튼에서 DelegateCommand로 명령 매개 변수를 받아들입니다.

어떤 버튼을 클릭했는지 알 수있는 방법을 알고 싶습니다. 내 VM에 단추와 관련된 개체를 전달할 수 싶어요.

내 XAML :

<ItemsControl x:Name="list" ItemsSource="{Binding ChemList}"> //ChemList is observable collection of objects 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Button Margin="5" 
        Command="{Binding ElementName=list,Path=DataContext.OnBtnSelect}" 
        CommandParameter="{Binding}"> 
       <Button.Content> 
        <StackPanel Orientation="Horizontal"> 
         <TextBlock Text="{Binding name}"/> 
         <TextBlock Text=" "/> 
         <TextBlock Text="{Binding num}"/> 
        </StackPanel> 
       </Button.Content> 
      </Button> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

내보기 모델 : DelegateCommand 생성자 매개 변수로

Action<object>  

을 받아 들여야

public DelegateCommand OnBtnSelect { get; private set; } 


In the constructor: 
OnBtnSelect = new DelegateCommand(OnSelect); 


public void OnSelect() 
{ 
     //How do i get here the object associated with the clicked button? 
} 
+0

프리즘을 사용하고 있습니까? –

+0

예 프리즘을 사용하고 있습니다 – ilmenite

+0

좋아, 내 게시물보기 –

답변

1
public DelegateCommand<object> OnBtnSelect { get; private set; } 

public void OnSelect(object args) 
{ 
    //If your binding is correct args should contains the payload of the event 
} 

//In the constructor 
OnBtnSelect = new DelegateCommand<object>(OnSelect);