Question

Situation:

  • 1 Combobox (A) is bound to a VM which has an ObservableCollection property of a model entity type.
  • 1 other Combobox (B) is also bound as just described, but to another model entity type.
  • My VM has a Command (RelayCommand by Josh Smith) which will fill the property ComboBox (B) is bound to

Problem:

  • Combobox has no Command to bind to
  • I am not supposed to break the MVVM-way
  • <i:Interaction.Triggers>...<i:InvokeCommandAction... / has not worked out
  • Also had no succes implementing a SelectionChangedBehaviour class with DependencyProperty

I feel like I'm not architecting this correctly, could someone please steer me into the right direction?

Code: Inside ViewModel:

This is the property Combobox A is bound to

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

This is the property Combobox B is bound to

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

Command I wish ComboBox A to bind to (So ComboBox B will get all the data based on a value selected in ComboBox A this is the main objective)

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

This is the code that needs to execute via the Command

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

Thanks in advance!

Était-ce utile?

La solution

You can add a custom behavior that attaches to the combobox and subscribes to selection changed.

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); }
        }
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top