Using an interaction trigger to invoke the visibility of a modified WPF method

What I would like to find out are two things: how to get a trigger that occurs when user control visibility changes, and pass the visibility value through the parameter.

For some reason, the trigger does not fire. I just added a ControlVisible to the parameter to show what I would like to happen when testing it was not there and it just had a built-in message box when visibility changed, as in the comment method.

I am using 4.0 with Visual Studio 2010

View of the main window containing the user control

<Window x:Class="bt.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="clr-namespace:bt" xmlns:ctrls="clr-namespace:bt.Controls" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" mc:Ignorable="d"> <Grid> <ctrls:Login Visibility="{Binding DataContext.Vis,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window},Converter={StaticResource BooleanToVisibilityConverter}}" > <i:Interaction.Triggers> <i:EventTrigger EventName="IsVisibleChanged"> <ei:CallMethodAction MethodName="VisibleTrigger" /> </i:EventTrigger> </i:Interaction.Triggers> </ctrls:Login> </Grid> </Window> 

UserControl View Model:

 namespace bt.Controls { class LoginViewModel { public LoginViewModel() { } public void VisibleTrigger(bool ControlVisible) { if (ControlVisible) { MessageBox.Show("Start timer"); } else { MessageBox.Show("Stop timer"); } } //public void VisibleTrigger() //{ // MessageBox.Show("Changed"); //} } } 
+3
source share
1 answer

First, we need to set the TargetObject property to viewmodel / DataContext, because the method that is being called is available in view mode:

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

Secondly, EventTrigger does not seem to work specifically with the IsVisibleChanged event. So the code snippet above works for another event, but not IsVisibleChanged. We can find a workaround in the answer to this SO question , using PropertyChangedTrigger to listen for the Visibility property, instead of listening for the 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 does not seem to provide a way to pass a parameter to a method. To be able to call a method with a parameter, it is better to use InvokeCommandAction instead of CallMethodAction, as suggested here , as well as suggested by @Rohit in your previous question .

+3
source

All Articles