Search code examples
wpfblendinteractioneventtrigger

PropertyChanged event on viewmodel property


i'm trying to Raise a PropertyChanged event on a Property in my ViewModel using interaction triggers .

CS :

public string MyContentProperty
{
    get { return "I Was Raised From an outside Source !";}
}

XAML :

<Button Content="{Binding MyContentProperty}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Button.Click">
               < .... what needs to be done ?>         
        </i:EventTrigger>                        
     </i:Interaction.Triggers>
</Button>

of course if there was any doubt for this question you have references to

 xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" 
 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 

at your disposal , thanks in advance .


Solution

  • You can use a normal command or Expression Blend's CallMethodAction, InvokeCommandAction or ChangePropertyAction.

    Here are four ways to do what you want:

    <Button Content="Button" Height="23" Width="100" Command="{Binding RaiseItCmd}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <i:InvokeCommandAction Command="{Binding RaiseItCmd}"/>
                <ei:CallMethodAction MethodName="RaiseIt" TargetObject="{Binding}"/>
                <ei:ChangePropertyAction Value="" 
                         PropertyName="MyContentProperty" TargetObject="{Binding}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>
    

    Here I'm using MVVM Light's ViewModelBase:

    using System.Windows.Input;
    using GalaSoft.MvvmLight;
    using Microsoft.Expression.Interactivity.Core;
    
    public class ViewModel : ViewModelBase
    {
        public ViewModel()
        {
            RaiseItCmd = new ActionCommand(this.RaiseIt);
        }
    
        public string MyContentProperty
        {
            get
            {
                return "property";
            }
            set
            {
                this.RaiseIt(); 
            }
        }
    
        public void RaiseIt()
        {
            RaisePropertyChanged("MyContentProperty");
        }
    
        public ICommand RaiseItCmd { get; private set; }
    }