从 ItemsControl (WPF MVVM) 触发事件

Triggering an event from ItemsControl (WPF MVVM)

我正在使用 WPF MVVM 设计模式。我需要从使用 ItemsControl 创建的文本框引发 PreviewKeyDown 事件。我能够将项目添加到集合 SourceCollection,但无法使用交互触发器触发 PreviewKeyDown 事件。对我在 xaml 中可能遗漏的任何想法表示赞赏 :) 这是我的代码:

MainWindow.xaml

    <Grid>
        <ItemsControl ItemsSource="{Binding SourceCollection}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Vertical"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding CollectionText}" Foreground="{Binding Path=ForegroundColor}"
                             FontSize="16" FontWeight="ExtraBold" FontFamily="Courier New">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="PreviewKeyDown">
                                <i:InvokeCommandAction Command="{Binding KeyDownAction}"></i:InvokeCommandAction>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </TextBox>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>

MainWindowViewModel.cs

public class MainWindowViewModel
{
    MainWindowModel model = new MainWindowModel();

    private ObservableCollection<MainWindowModel> _SourceCollection;
    public ObservableCollection<MainWindowModel> SourceCollection
    {
        get { return _SourceCollection; }
        set { _SourceCollection = value; }
    }

    public MainWindowViewModel()
    {
        SourceCollection = new ObservableCollection<MainWindowModel>();

        for (int i = 0; i < 4; i++)
        {
            model = new MainWindowModel();
            model.CollectionText = "This is line " + i;
            if (i % 2 == 0)
            { model.ForegroundColor = Brushes.Blue; }
            else
            { model.ForegroundColor = Brushes.Green; }
            SourceCollection.Add(model);
        }
    }

    public RelayCommand<KeyEventArgs> KeyDownAction
    {
        get { return new RelayCommand<KeyEventArgs>(KeyDownMethod); }
    }

    private void KeyDownMethod(KeyEventArgs e)
    {
        //some code here 
    }
}

您的 WindowModel 中应该有以下 ICommand 定义,而不是 MainWindowViewModel。

 public RelayCommand<KeyEventArgs> KeyDownAction
        {
            get { return new RelayCommand<KeyEventArgs>(KeyDownMethod); }
        }

        private void KeyDownMethod(KeyEventArgs e)
        {
            //some code here 
        }

因为您的项目控件的项目模板的数据上下文是项目而不是主视图模型。

您在 "InvokeCommandAction" 中的命令 "Binding" 不正确。它不在单个集合项上,而是在 ViewModel 级别。将其更改为:

<i:InvokeCommandAction 
  Command="{Binding RelativeSource={RelativeSource AncestorType=Window}, 
  Path=DataContext.KeyDownAction}">
</i:InvokeCommandAction>

这样您就指向了 ViewModel 中定义的命令。