gpt4 book ai didi

data-binding - 将 Combobox SelectionChanged 绑定(bind)到命令 MVVM

转载 作者:行者123 更新时间:2023-12-01 03:50:09 25 4
gpt4 key购买 nike

情况:

  • 1 Combobox (A) 绑定(bind)到具有模型实体类型的 ObservableCollection 属性的 VM。
  • 1 个其他 Combobox (B) 也如刚刚描述的那样绑定(bind),但绑定(bind)到另一个模型实体类型。
  • 我的虚拟机有一个命令(Josh Smith 的 RelayCommand),它将填充属性 ComboBox (B) 绑定(bind)到

  • 问题:
  • 组合框没有绑定(bind)到
  • 的命令
  • 我不应该打破 MVVM 方式
  • <i:Interaction.Triggers>...<i:InvokeCommandAction.../还没解决
  • 也没有成功实现具有 DependencyProperty
  • 的 SelectionChangedBehaviour 类

    我觉得我的架构设计不正确,有人可以引导我走向正确的方向吗?

    代码:
    内部 View 模型:

    这是 Combobox A 绑定(bind)的属性

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

    这是 Combobox B 绑定(bind)的属性

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

    我希望 ComboBox A 绑定(bind)到的命令(因此 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;
    }
    }

    这是需要通过命令执行的代码

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

    关于data-binding - 将 Combobox SelectionChanged 绑定(bind)到命令 MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23562224/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com