C# 从ItemsControl(WPF MVVM)触发事件

C# 从ItemsControl(WPF MVVM)触发事件,c#,wpf,mvvm,itemscontrol,eventtrigger,C#,Wpf,Mvvm,Itemscontrol,Eventtrigger,我使用的是WPF MVVM设计模式。我需要从使用ItemsControl创建的文本框引发PreviewKeyDown事件。我可以将项目添加到集合SourceCollection,但无法使用交互触发器触发PreviewKeyDown事件。任何关于我在xaml中可能缺少的内容的想法都非常感谢:)以下是我的代码: MainWindow.xaml <Grid> <ItemsControl ItemsSource="{Binding SourceCollectio

我使用的是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 
    }
}
公共类MainWindowViewModel
{
MainWindowModel模型=新的MainWindowModel();
私有ObservableCollection\u SourceCollection;
公共可观测集合SourceCollection
{
获取{return\u SourceCollection;}
设置{u SourceCollection=value;}
}
公共主窗口视图模型()
{
SourceCollection=新的ObservableCollection();
对于(int i=0;i<4;i++)
{
模型=新的MainWindowModel();
model.CollectionText=“这是行”+i;
如果(i%2==0)
{model.ForegroundColor=笔刷.Blue;}
其他的
{model.ForegroundColor=笔刷.Green;}
SourceCollection.Add(模型);
}
}
公共中继命令键关闭操作
{
获取{returnnewrelaycommand(KeyDownMethod);}
}
私有void keydown方法(KeyEventArgs e)
{
//这里有一些代码
}
}

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

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

        private void KeyDownMethod(KeyEventArgs e)
        {
            //some code here 
        }
public RelayCommand键关闭操作
{
获取{returnnewrelaycommand(KeyDownMethod);}
}
私有void keydown方法(KeyEventArgs e)
{
//这里有一些代码
}
因为itemcontrol的项模板的datacontext是项而不是主视图模型。

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

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


通过这种方式,您可以指向ViewModel中定义的命令。

我认为无法捕获事件的原因是因为文本框的DataContext实际上是ItemsControl的ItemsSource。您需要将您的命令与RelativeSource@Stojdza绑定。是的,现在可以理解了。谢谢:)
 public RelayCommand<KeyEventArgs> KeyDownAction
        {
            get { return new RelayCommand<KeyEventArgs>(KeyDownMethod); }
        }

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