简体   繁体   中英

WPF ComboBox SelectionChanged command not firing

I have a ComboBox,and I define its ItemTemplate.I want the combobox selectionChanged command firing when clicking the combobox item or check/uncheck the checkbox before it.Here is the xmal:

<ComboBox   x:Name="DeptComboBox"                    
                Grid.Row="2" Grid.Column="3"  
                IsReadOnly="True"
                StaysOpenOnEdit="True"
                ItemsSource="{Binding DeptDtoes}" 
                Text="{Binding SelectedDeptNames}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding DeptSelectedCommand}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding Name}" IsChecked="{Binding IsSelected}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

And the ViewModel:

public class AddDoctorViewModel:ViewModelBase, ISingletonDependency
{
    private readonly IBasicAppService _basicAppService;

    public ObservableCollection<DeptDto> DeptDtoes { get; }

    private string _selectedDeptNames;
    public string SelectedDeptNames
    {
        get { return _selectedDeptNames; }
        set
        {
            _selectedDeptNames = value;
            RaisePropertyChanged(nameof(SelectedDeptNames));
        }
    }

    private int _selectedIndex;
    public int SelectedIndex 
    {
        get { return _selectedIndex; }
        set 
        {
            _selectedIndex = value;
            RaisePropertyChanged(nameof(SelectedIndex));
        }
    }

    public RelayCommand DeptSelectedCommand { get; set; }

    public AddDoctorViewModel(IBasicAppService basicAppService) 
    {
        _basicAppService = basicAppService;
        DeptDtoes = new ObservableCollection<DeptDto>(_basicAppService?.DeptDtoes);            
        DeptSelectedCommand = new RelayCommand(DeptSelected);
    }

    private void DeptSelected() 
    {

    }
}

But the combobox selectionChanged command not firing.Can anyone help me?

       <ComboBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding Name}" IsChecked="{Binding IsSelected}"
                          Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ComboBox}},Path=DataContext.DeptSelectedCommand}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM