ListCheckBox中DataTemplate的CallMethodAction抛出了一个异常“无法在类型为”的对象
本文关键字:异常 类型 对象 一个 CallMethodAction DataTemplate ListCheckBox | 更新日期: 2023-09-27 17:54:41
我试图使用交互,但我不能在ViewModel的方法中编写有序的参数。我使用一个ListBox,里面有一个复选框和另一个控件。所以我试图在我的usercontrol中使用CallMathodAction的params TargetObject的不同组合,但是我不能从我的ListBox中获得Item的对象,当CheckBox是"未选中的"。
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");
}
当我尝试使用上面描述的代码时,我得到了一些异常:抛出:
"无法在类型为' checkedlistitem1 '的对象上找到匹配期望签名的名为'PropertyChecked'的方法。"(System.ArgumentException)
异常消息= "无法在类型为'CheckedListItem ' 1'的对象上找到与期望签名匹配的名为'PropertyChecked'的方法",异常类型= "系统。
从消息中,我认为,c#方法有一个坏的输入参数,我知道ListBox中的Item有类型为CheckedListItem<TProperty>的对象,我开始尝试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类元素,但我需要获得类型为checkedlisttem <TProperty>的对象。
我发现如何用命令机制做这样的事情,但我只想使用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
对某些签名非常严格,不允许其他格式。这个问题已经有了答案,但是在我看来,没有一个是适合这里的。
我能想到的唯一解决方法是将自定义参数绑定到一个更容易访问的项目,例如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>;
}
对不起,我只能回答你的问题了,也许还有更好的答案。