-->

Why the TargetedTriggerAction's TargetObject i

2019-09-13 07:17发布

问题:

I have three Buttons (1.Male,2.Femal,3.OK),I want to change the CommandParameters Property of Button(OK) by Click Male Button or FeMale Button. I found the question Setting a property with an EventTrigger is very helpful to me, but the TargetObject of TargetedTriggerAction<FrameworkElement> in void Invoke(object parameter){...} function is always null, WHY ?

HOW Can I to solve the Problem?

Error info in output window.

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=OKGenderButton'. BindingExpression:(no path); DataItem=null; target element is 'SetterAction' (HashCode=20203785); target property is 'TargetObject' (type 'Object')

回答1:

As the error indicated, bindingExpression has no path. Try to add a Path

{Binding Path=OKGenderButton }


回答2:

As the question ElementName Binding is failingmentioned,

The code Source={x:Reference DisplayMarkers} works

Demo Code:

<Button Grid.Row="2" Grid.Column="1"
                        Content="FeMale"
                        Margin="0 0 90 0"
                        VerticalAlignment="Bottom"
                        HorizontalAlignment="Right"
                        Width="93"
                        Height="93"
                        Style="{DynamicResource FemaleButton}">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <triggerActions:SetterAction PropertyName="CommandParameter"
                                                         TargetObject="{x:Reference OKGenderButton}"
                                                         Value="{x:Static constants:Genders.FeMale}" />

                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>