Using interaction trigger to call visibility changed method WPF

前端 未结 1 970
孤独总比滥情好
孤独总比滥情好 2021-01-13 11:45

What I would like to figure out is two things, how to get a trigger occurring when a user control\'s visibility is changed and passing the value of visibility through as a p

相关标签:
1条回答
  • 2021-01-13 12:15

    First, we need to set TargetObject property to viewmodel/DataContext, because method to be invoked is available in the viewmodel :

    ......
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="IsVisibleChanged">
            <ei:CallMethodAction MethodName="VisibleTrigger" TargetObject="{Binding}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    ......
    

    Second, EventTrigger doesn't seems to work specifically with IsVisibleChanged event. So code snippet above works for other event, but not IsVisibleChanged. We can find a workaround in the answer to this SO question, by using PropertyChangedTrigger to listen to Visibility property changed, instead of listening to IsVisibleChanged event :

    <i:Interaction.Triggers>
        <ei:PropertyChangedTrigger Binding="{Binding Visibility, ElementName=MyControlName}">
            <ei:CallMethodAction  MethodName="VisibleTrigger" TargetObject="{Binding}"/>
        </ei:PropertyChangedTrigger>
    </i:Interaction.Triggers>
    

    Third, CallMethodAction doesn't seems to provide a way to pass parameter to the method. To be able to invoke a method with parameter we better use InvokeCommandAction instead of CallMethodAction as suggested here and also suggested by @Rohit in your previous question.

    0 讨论(0)
提交回复
热议问题