0

I have bound the Checked event of a checkbox to a method. I am also passing two Command Parameters to that method through a converter (implementing IMultiValueConverter). In the code below, the two CommandParameter bindings are of type bool and type string respectively.

<CheckBox Name="cbTopLeftHeader" Content="Top Left Header"> 
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Checked">
                <i:InvokeCommandAction Command="{Binding IncludeCheckedCommand}">
                    <i:InvokeCommandAction.CommandParameter>
                        <MultiBinding Converter="{StaticResource MultiBindingConverter}" ConverterParameter="IncludeChecked">
                            <Binding ElementName="cbTopLeftHeader" Path="IsChecked"></Binding>
                            <Binding Source="{StaticResource TopLeft}"></Binding>
                        </MultiBinding>
                    </i:InvokeCommandAction.CommandParameter>
                </i:InvokeCommandAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>
</CheckBox>

Why is it that if I replace the line:

<Binding ElementName="cbTopLeftHeader" Path="IsChecked"></Binding>

With:

<Binding RelativeSource="{RelativeSource Self}" Path="IsChecked"></Binding>

In my converter the IsChecked parameter changes from type bool to type DependencyProperty (DependencyProperty.UnsetValue)?

How can I achieve passing the Checked property without having the element bind to itself by name?

Doug
  • 41
  • 9
  • 1
    You are not binding the CommandParamter property of the CheckBox (as e.g. [here](https://stackoverflow.com/q/7617375/1136211)), hence `RelativeSource Self` is not the CheckBox. – Clemens Sep 05 '17 at 05:26

1 Answers1

1

Get rid the interaction trigger and use the Command and CommandParameter properties of the CheckBox:

<CheckBox Name="cbTopLeftHeader" Content="Top Left Header" Command="{Binding IncludeCheckedCommand}">
    <CheckBox.CommandParameter>
        <MultiBinding Converter="{StaticResource MultiBindingConverter}" ConverterParameter="IncludeChecked">
            <Binding RelativeSource="{RelativeSource Self}" Path="IsChecked"/>
            <Binding Source="{StaticResource TopLeft}"></Binding>
        </MultiBinding>
    </CheckBox.CommandParameter>
</CheckBox>

Or stick with your current approach of binding to the CheckBox using ElementName. {RelativeSource Self} of an InvokeCommandAction is not a CheckBox.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thank you mm8. I did not realize (although seems obvious now) that the command for the checkbox by default is the `Checked` event. Your answer and Clemens comment above answer my questions. – Doug Sep 05 '17 at 23:31