2017-11-29 4 views
1

단추가 들어있는 목록 상자가 있습니다. 아무 버튼도 추가 할 수 있습니다. 나는 C#을 통해 "AddedButtonList"라는 목록에 버튼을 추가하고 다음과 같은 그 목록에 바인딩 한 : 내가 코드에서 모든 속성을 결합했기 때문에클릭 한 목록 상자에서 항목의 색인을 얻는 방법 (클릭하지 않고 명령과 바인딩되는 단추가있는 목록)

<Grid Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="1" Grid.RowSpan="1"> 
     <Grid.Resources> 
      <DataTemplate DataType="{x:Type viewModel:AddedAction}"> 
       <Button Content="{Binding Title}" 
         Height="40" 
         Width="100" 
         Command="{Binding Command}"> 
       </Button> 
      </DataTemplate> 
     </Grid.Resources> 
     <ListBox ItemsSource="{Binding actionsRecordVmObj.AddedActionsList}" Width="Auto"> 
     </ListBox> 
    </Grid> 

우리가 XAML에서 위의 코드를 사용하여 버튼 중 하나를 더 추가 할 수 있습니다 뒤에. 코드 뒤에 다음과 같습니다

public abstract class AddedAction 
    { 
     public bool IsDisable { get; set; } 
     public string Title { get; set; } 
     public string ButtonIndex { get; set; } 
     public abstract ICommand Command { get; } 
    } 



public class AddedSourceFileActionVm : AddedAction 
    { 
     public ICommand _command; 
     //constructor 
     public AddedSourceFileActionVm() 
     { 
      Title = "Source File"; 
      _command = new RelayCommand(p => AddedSourceFileActionCommandExecuted(null), p => CanAddedSourceFileActionCommandExecute()); 
     } 

이 모든 버튼이 명령 (버튼이 목록에 반복 할 수있다)과 결합된다. 눌려진 버튼 (목록의 항목)의 색인을 가져 오려고합니다. 일부는 AlternationCount를 사용하여 많은 답변을 읽었지 만 코드에서 색인을 원할 때는 명령을 바인딩에 사용하고 클릭 이벤트로 표시 했으므로이를 수행 할 수 없습니다. 내가 MVVM을 사용하고 있기 때문에이

private void lstButton_Click(object sender, RoutedEventArgs e) 
    { 
       Button button = sender as Button;   
       int index = _myListBoxName.Items.IndexOf(button.DataContext); 
     } 

를 사용하지 못할 내가 명령으로 모든 버튼을 바인딩합니다. 이에 대한 해결책을 제안하십시오.

또는 짧은 목록에서 누르는 버튼의 색인을 얻는 방법은 무엇입니까?

+0

당신이 할 수있는 CommandParameter = "{Binding}"에 의해 현재 DataContext를 Button의 CommandParameter에 전달합니다. Command의 Execute 메소드의'object' 매개 변수로 전달됩니다. – Clemens

답변

3

여기 쉽게 인덱스를 얻을 수있는 방법은 사전에 감사합니다 .... :
1.이 abstractevent 당신의 AddedAction에 정의합니다. AddedAction 예를
3. 올립니다를 만들 때
2. event를 구독 eventAddedAction.Command
4. 이벤트 핸들러 예를 들어

에 인덱스를 가져 오기 실행시 :

public abstract class AddedAction 
{ 
    //define the event 
    public abstract event EventHandler CommandExecuted; 

    public abstract ICommand Command { get; } 
    //... 
} 
public class AddedSourceFileActionVm : AddedAction 
{ 
    public override event EventHandler CommandExecuted; 

    private void AddedSourceFileActionCommandExecuted(object obj) 
    { 
     //invoke the event 
     CommandExecuted?.Invoke(this, null); 
     //... 
    } 
    //... 
} 
public class ActionsRecordVm 
{ 
    public List<AddedAction> AddedActionsList { get; } = new List<AddedAction>(); 
    public void AddNewAddedAction() 
    { 
     var addedAction = new AddedSourceFileActionVm(); 
     //Subscribe to the event 
     addedAction.CommandExecuted += AddedAction_CommandExecuted; 
     AddedActionsList.Add(addedAction); 
    } 

    private void AddedAction_CommandExecuted(object sender, EventArgs e) 
    { 
     //get the index 
     int index = AddedActionsList.IndexOf((AddedAction)sender); 

     //... 
    } 
    //... 
}