C# 通过交互将按钮单击事件绑定到ViewModel中的方法。在运行时动态创建按钮时触发

C# 通过交互将按钮单击事件绑定到ViewModel中的方法。在运行时动态创建按钮时触发,c#,wpf,mvvm,binding,C#,Wpf,Mvvm,Binding,在我的C#WPF MVVM模式应用程序中,我的视图中有一个ItemsControl,它基于绑定的ItemsSource在画布上绘制线条和按钮,在XAML中定义如下: <Window.DataContext> <viewmodels:MainWindowViewModel /> </Window.DataContext> . . . <ItemsControl x:Name="DiagramViewCanvas"

在我的C#WPF MVVM模式应用程序中,我的视图中有一个ItemsControl,它基于绑定的ItemsSource在画布上绘制线条和按钮,在XAML中定义如下:

<Window.DataContext>
    <viewmodels:MainWindowViewModel />
</Window.DataContext>

.
.
.

<ItemsControl
    x:Name="DiagramViewCanvas"
    ItemsSource="{Binding DiagramObjects, UpdateSourceTrigger=PropertyChanged}">

    <ItemsControl.Resources>
        <DataTemplate DataType="{x:Type local:LineObject}">
            <Line
                X1="{Binding XStart}"
                Y1="{Binding YStart}"
                X2="{Binding XEnd}"
                Y2="{Binding YEnd}"
                Stroke="White"
                StrokeThickness="1"
                SnapsToDevicePixels="True"/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:ButtonObject}">
            <Button
                Style="{DynamicResource MyDiagramButtonStyle}"
                Width="225"
                Height="30"
                Content="{Binding Content}"
                FontSize="13"
                SnapsToDevicePixels="True">
            </Button>
        </DataTemplate>
    </ItemsControl.Resources>
    
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas Background="Black" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="Canvas.Left" Value="{Binding XPosition, UpdateSourceTrigger=PropertyChanged}" />
            <Setter Property="Canvas.Top" Value="{Binding YPosition, UpdateSourceTrigger=PropertyChanged}" />
        </Style>
    </ItemsControl.ItemContainerStyle>

</ItemsControl>
我得到以下错误:

System.ArgumentException: 'Could not find method named 'OnButtonClick' on object of type 'ButtonObject' that matches the expected signature.'
问题1:我在实现交互触发器时是否犯了一个基本错误(我的代码中有许多其他交互触发器可以完全正常工作)?或者是Interaction.Triggers在运行时动态创建按钮的情况下不起作用

问题2:我应该改用ICommand吗(例如中提到的)


感谢您提供我做错了什么的指导。

找到了使用交互的解决方案。触发器:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Click">
        <i:CallMethodAction TargetObject="{Binding RelativeSource={RelativeSource AncestorType={x:Type Canvas}}, Path=DataContext}" MethodName="OnButtonClick"/>
    </i:EventTrigger>
</i:Interaction.Triggers>


“我希望方法OnButtonClick驻留在我的ViewModel中”,因此您基本上希望抛弃mvvm的所有原则。wpf将为您提供每一步,很好地解决了单击事件处理程序和触发器的问题。只需在
ButtonObject
类中包含一个
ICommand
项,并直接绑定到该项即可。
public void OnButtonClick(object sender, RoutedEventArgs e)
{
    if (sender is Button btn)
    {
        // do something
    }
}
System.ArgumentException: 'Could not find method named 'OnButtonClick' on object of type 'ButtonObject' that matches the expected signature.'
<i:Interaction.Triggers>
    <i:EventTrigger EventName="Click">
        <i:CallMethodAction TargetObject="{Binding RelativeSource={RelativeSource AncestorType={x:Type Canvas}}, Path=DataContext}" MethodName="OnButtonClick"/>
    </i:EventTrigger>
</i:Interaction.Triggers>