【问题标题】:WPF ComboBox SelectionChanged command not firingWPF ComboBox SelectionChanged 命令未触发
【发布时间】:2019-11-22 03:52:31
【问题描述】:

我有一个 ComboBox,我定义了它的 ItemTemplate。我希望在单击组合框项或选中/取消选中它之前的复选框时触发组合框 selectionChanged 命令。这里是 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>

还有 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() 
    {

    }
}

但是组合框 selectionChanged 命令没有触发。谁能帮助我?

【问题讨论】:

  • (stackoverflow.com/questions/8666256/…) 这可能会对您有所帮助.. 我想您在 xaml 中错过了 SelectedItem="{Binding someitems}"
  • 不,它不起作用,我认为可能是 一些如何停止 SelectionChanged cammand 触发。如果我​​不设置组合框的 部分,该命令将起作用。

标签: c# wpf combobox


【解决方案1】:
       <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>

【讨论】:

  • 非常感谢,我已将您的答案标记为解决方案。
猜你喜欢
  • 2014-05-02
  • 2014-05-08
  • 1970-01-01
  • 2021-03-17
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 1970-01-01
  • 2019-04-05
相关资源
最近更新 更多