2017-10-25 10 views
0

ComboBox이있는 wpf 프로젝트가 있습니다. 내부의 항목은 동적으로 채워집니다. 그래서 그것은 Label과 명령을 포함하는 모델에 묶여있다.WPF의 ComboBoxItem에 대한 바인딩 명령

사용자가 드롭 다운/ComboBox에서 항목을 선택하면 명령이 실행되어야합니다. 명령에 TextBlockHyperlink이 포함 된 DataTemplate으로 시도했습니다. 그러나 Command는 Label (Hyperlink)을 선택하고 전체 항목을 클릭하는 경우에만 실행됩니다.

<ComboBox ItemsSource="{Binding Path=States}" SelectedItem="{Binding CurrentState}" > 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <TextBlock> 
       <Hyperlink Command="{Binding Command}" TextDecorations="None" Foreground="Black"> 
        <TextBlock Text="{Binding Path=Label}"/> 
       </Hyperlink> 
      </TextBlock> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 

그래서 질문은 지금, 어떻게 ComboBoxItem 내 명령을 결합 할 수있다?

답변

1

ComboBoxItem는 더 Command 속성이 없습니다하지만 당신은 CurrentState 재산의 세터에서 명령을 실행할 수 있습니다 :

당신이 ComboBox에서 항목을 선택할 때마다이 속성이 설정됩니다
private State _currentState; 
public State CurrentState 
{ 
    get { return _currentState; } 
    set 
    { 
     _currentState = value; 
     if (_currentState != null) 
     { 
      _currentState.Command.Execute(null); 
     } 
    } 
} 

. 다른 옵션은보기에서 SelectionChanged 이벤트를 처리하는 것입니다.

+0

고맙습니다! –