数据绑定 – 将Combobox SelectionChanged绑定到命令MVVM

情况:

> 1 Combobox(A)绑定到具有模型实体类型的ObservableCollection属性的VM.
> 1其他Combobox(B)也如上所述绑定,但是另一个模型实体类型.
>我的VM有一个命令(Josh Smith的RelayCommand),它将填充属性ComboBox(B)必然会

问题:

> Combobox没有要绑定的命令
>我不应该打破MVVM的方式
>< i:Interaction.Triggers> …< i:InvokeCommandAction … /尚未解决
>使用DependencyProperty也没有成功实现SelectionChangedBehaviour类

我觉得我没有正确地构建这个,有人可以引导我走向正确的方向吗?

码:
在ViewModel中:

这是Combobox A必须遵守的属性

private ObservableCollection<tblToModels> _modelRecords;
public ObservableCollection<tblToModels> modelRecords
{
    get { return _modelRecords; }
    set
    {
        _modelRecords = value;
        RaisePropertyChanged();
    }
}

这是Combobox B必须遵守的属性

private ObservableCollection<tblToCarTypes> _carTypeRecords;
public ObservableCollection<tblToCarTypes> carTypeRecords
{
    get { return _carTypeRecords; }
    set
    {
        _carTypeRecords = value;
        RaisePropertyChanged();
    }
}

命令我希望ComboBox A绑定到(所以ComboBox B将根据ComboBox A中选择的值获取所有数据这是主要目标)

private ICommand _searchByNameCommand;
public ICommand SearchByNameCommand
{
    get
    {
        if (_searchByNameCommand == null)
        {
            _searchByNameCommand = new RelayCommand(
                p => this.LoadCarTypeCollection(),
                p => { return (this.currentModel != null); }
            );
        }
        return _searchByNameCommand;
    }
}

这是需要通过Command执行的代码

private void LoadCarTypeCollection()
{
    var q = service.GetCarTypesByModelName(currentModel.Displayname);
    carTypeRecords = new ObservableCollection<tblToCarTypes>(q);
}

提前致谢!

最佳答案 您可以添加附加到组合框的自定义行为,并订阅已更改的选择.

using System.Windows.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Microsoft.Xaml.Interactivity;

namespace StackOverflowWin81
{
    public class SelectionChangedCommandBehavior : DependencyObject, IBehavior
    {
        private ComboBox _comboBox;

        public void Attach(DependencyObject associatedObject)
        {
            //set attached object
            _comboBox = associatedObject as ComboBox;

            //subscribe to event
            _comboBox.SelectionChanged += _comboBox_SelectionChanged;
        }

        private void _comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //execute the command
            if (this.Command.CanExecute(null))
            {
                Command.Execute(null);
            }
        }

        public void Detach()
        {
            _comboBox.SelectionChanged -= _comboBox_SelectionChanged;
        }

        public DependencyObject AssociatedObject { get; private set; }

        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
            "Command", typeof (ICommand), typeof (SelectionChangedCommandBehavior), new PropertyMetadata(default(ICommand)));

        public ICommand Command
        {
            get { return (ICommand) GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }
    }
}
点赞