WPF CallMethodAction for DataTemplate in ListCheckBox throws "Could not find method for object of type" exception

I am trying to use interactions but I am unable to write ordered parameters in my method in the ViewModel. I am using ListBox which has items inside a checkbox and other controls. So I'm trying to use different combinations of TargetObject CallMathodAction parameters in my usercontrol, but I can't get the Item object from my ListBox when the CheckBox is "unchecked".

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");
}

      

When I try to use the code as described above, I have some kind of exception: Thrown:

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

From the post, I think the C # method has parameters with bad inputs, I know that the item in the ListBox has an object of type CheckedListItem, and I'm starting to try different combinations of C # and Xaml code:

[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] worked with C # methods [0], [2] and [3], but when I debug information about what I have, I have a single CheckBox-like item, but I need to get an object of type CheckedListItem.

I found how to do something like this using the Command mechanism, but I only want to use CallMethod.

+3


source to share


1 answer


This is probably what you are looking for:

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

      

Edit

Passing parameters PropertyUnchecked

(for example PropertyUnchecked(object customParam, object sender, RoutedEventArgs e)

) was not as easy as I expected because it is CallMethodAction

very strict for certain signatures and does not allow other formats. There are already answers provided for this problem, however IMO none of them fit here.

The only workaround I could think of was to bind a custom parameter to a more accessible element, eg. CheckBox.Tag

as shown below:



                <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>

      

This will send the user data along with sender

into the method PropertyUnchecked

, making it easier to access:

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

      

Sorry, but this was as close as I could answer your question, maybe there are better answers out there.

+1


source







All Articles