情况:
问题:
<i:Interaction.Triggers>...<i:InvokeCommandAction...
/尚未成功我觉得我没有正确地构建这个,有人可以引导我走向正确的方向吗?
代码: 在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);
}
提前致谢!
答案 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); }
}
}
}