I'm trying to use interactions, but I can't write ordered params in my method in ViewModel. I'm use a ListBox who have items inner a checkbox and another controls. So I trying to use the different combinations of params TargetObject of CallMathodAction in my usercontrol, but I can't get the object of Item from my ListBox, when 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 used the code as described above, I've got a some exception: Thrown:

"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

From the message, I think, that the C# method have a bad inputs params, I know that Item in ListBox have object of type CheckedListItem<TProperty> and I'm begin to 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>

The Xaml [4] worked with C# methods [0],[2] and [3], but when I debug to know what the sender I have, I've got the only CheckBox like element but I need get the object of type CheckedListItem <TProperty>.

I'm found how to do something like this with a Command mechanism, but I want to use only CallMethod.

1

There are 1 answers

4
Bahman_Aries On BEST ANSWER

This is probably what you're looking for:

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

Edit

Passing parameters to PropertyUnchecked (e.g. PropertyUnchecked(object customParam, object sender, RoutedEventArgs e)) was not as easy as I expected because CallMethodAction is very strict on certain signatures and does not allow other formats. There are already answers provided for this issue however IMO none of them is suitable here.

The only workaround I could think of was to bind the custom parameter to a more accessible item e.g. CheckBox.Tag like 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 custom data along with sender to the PropertyUnchecked method, making the access easier:

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

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