ListCheckBox 中 DataTemplate 的 WPF 交互 CallMethodAction 抛出异常 "Could not find method on object of type"

WPF Interactions CallMethodAction for DataTemplate in ListCheckBox thrown a exception "Could not find method on object of type"

我正在尝试使用交互,但我无法在 ViewModel 的方法中编写有序参数。我使用的是一个 ListBox,它在复选框和另一个控件中包含项目。因此,我尝试在我的用户控件中使用 CallMathodAction 的参数 TargetObject 的不同组合,但是当 CheckBox 为 'unchecked' 时,我无法从我的 ListBox 中获取 Item 的对象。

XAML

<ListBox Name="PropertiesListBox"
                 ItemsSource="{Binding PropertiesItems}"
                 SelectedItem="{Binding SelectedPropertyItem}">
            <ListBox.ItemTemplate>
                <DataTemplate >
                    <DockPanel LastChildFill="True">
                               <CheckBox DockPanel.Dock="Left"
                                  IsChecked="{Binding IsChecked, Mode=TwoWay}">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Unchecked">
                                    <ei:CallMethodAction  MethodName="PropertyUnchecked"
                                        TargetObject="{Binding}"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </CheckBox>
                        <TextBlock DockPanel.Dock="Left"
                                   Text="{Binding Path=Item.FieldsProperty.Name}"
                                   Margin="3,0,0,0"/>
                        <Canvas DockPanel.Dock="Right">
                            <Path Width="20" 
                                  Height="20" 
                                  Stretch="Fill" 
                                  Fill="{DynamicResource CommonBorderButtonBorderMouseOnBrush}" 
                                  Data="{StaticResource NextBtnGeometry}" />
                        </Canvas>
                        <StackPanel/>
                    </DockPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

C# [0]

public void PropertyUnchecked()
{
    MessageBox.Show("Unchecked");
}

当我尝试使用上述代码时,出现了一些异常: 抛出:

"Could not find method named 'PropertyChecked' on object of type 'CheckedListItem 1' that matches the expected signature."(System.ArgumentException)
Exception Message = "Could not find method named 'PropertyChecked' on object of type 'CheckedListItem`1' that matches the expected signature.", Exception Type = "System.ArgumentException", Exception WinRT Data = null

从消息中,我认为 C# 方法的输入参数错误,我知道 ListBox 中的 Item 具有类型为 CheckedListItem 的对象,我开始尝试 C# 和 Xaml代码:

[1]

public void PropertyUnchecked(CheckedListItem<TProperty>tr, object sender, RoutedEventArgs e)
{
    MessageBox.Show("Unchecked");
}

[2]

public void PropertyUnchecked(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Unchecked");
}

[3]

public void PropertyUnchecked(object sender)
{
    MessageBox.Show("Unchecked");
}

[4]

<CheckBox DockPanel.Dock="Left"
          IsChecked="{Binding IsChecked, Mode=TwoWay}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Unchecked">
            <ei:CallMethodAction MethodName="PropertyUnchecked"
                TargetObject="{Binding ElementName = "PropertiesListBox", Path = "DataContex"}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</CheckBox>

Xaml [4] 使用 C# 方法 [0]、[2] 和 [3],但是当我调试以了解我的发件人时,我得到了唯一的 CheckBox元素,但我需要获取 CheckedListItem 类型的对象。

我找到了如何使用 Command 机制做这样的事情,但我只想使用 CallMethod。

这可能是您要查找的内容:

public void PropertyUnchecked(object sender, RoutedEventArgs e)
{
    var item = ((ContentPresenter)((CheckBox)e.Source).TemplatedParent).Content as CheckedListItem<TProperty>;
}

编辑

将参数传递给 PropertyUnchecked(例如 PropertyUnchecked(object customParam, object sender, RoutedEventArgs e))并不像我预期的那么容易,因为 CallMethodAction 对某些签名非常严格并且不允许其他格式。已经为这个问题提供了 answers,但是 IMO none 适合这里。

我能想到的唯一解决方法是将自定义参数绑定到更易于访问的项目,例如CheckBox.Tag 如下所示:

                <CheckBox DockPanel.Dock="Left"
                          IsChecked="{Binding IsChecked, Mode=TwoWay}"
                          Tag="{Binding}">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Unchecked">
                            <ei:CallMethodAction  MethodName="PropertyUnchecked" 
                                TargetObject="{Binding}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </CheckBox>

这会将自定义数据与 sender 一起发送到 PropertyUnchecked 方法,使访问更容易:

public void PropertyUnchecked(object sender, EventArgs e)
{
    var Item = ((CheckBox)sender).Tag as CheckedListItem<TProperty>;
}

抱歉,这是我所能回答的最接近的问题,也许还有更好的答案。