将Combobox SelectionChanged绑定到命令MVVM

时间:2014-05-09 10:24:15

标签: data-binding mvvm combobox command

情况:

  • 1 Combobox(A)绑定到具有模型实体类型的ObservableCollection属性的VM。
  • 另外1个Combobox(B)也按照刚刚描述的方式绑定,但是另一个模型实体类型。
  • 我的VM有一个命令(Josh Smith的RelayCommand),它将填充属性ComboBox(B)必然

问题:

  • Combobox没有要绑定的命令
  • 我不应该打破MVVM-way
  • <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);
}

提前致谢!

1 个答案:

答案 0 :(得分:2)

您可以添加附加到组合框的自定义行为,并订阅更改的选择。

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); }
        }
    }
}