-4

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 .

eran otzap
  • 12,293
  • 20
  • 84
  • 139
  • Why do you want to do this? You are effectively adding logic into your view. Why can't you bind the button command to an ICommand and then raise your property changed from thew ViewModel? – Meirion Hughes Mar 21 '13 at 18:37
  • because the logic is view related , besides why was not the question , how was the question . – eran otzap Mar 21 '13 at 18:40
  • There is no possible need to do this. – Kapitán Mlíko Mar 21 '13 at 21:18
  • @ViktorLaCroix how could you state that in such confidence i wan't to Raise a PropertyChanged event on a Calculated Property with out have to bind my button to a command property , implement a method for the command , just inorder to get one line of code to occur , the example here is arbitrary and not a real scenario the key of asking a good question is to precise , my logic is of no interest , and again i'm asking HOW and if this is possible , and not WHY . – eran otzap Mar 21 '13 at 21:46
  • "Raise a PropertyChanged event on a Calculated Property" you calculate in XAML or what? You only raising PropertyChanged in VM to notify V about the change. When you change something in V then it's changed there and thus there is no need to raise that event. When you do changes in V you only choose when to [UpdateSourceTrigger](http://msdn.microsoft.com/en-us/library/system.windows.data.binding.updatesourcetrigger.aspx) ...you are not raising the event. Or maybe provide valid example and not meaningless and senseless one. – Kapitán Mlíko Mar 21 '13 at 23:44
  • @ViktorLaCroix you kinda missed the point , i'll use an example public int result { get{ return X + Y ;} } now i wan't the result to be displayed in XAML , in some point in time when i need to know if X or Y changed. so know i Raise a ProertyChanged(.. , Args("Result")); and the property will refresh it's value via binding . – eran otzap Mar 22 '13 at 05:21

1 Answers1

2

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; }
}
Phil
  • 42,255
  • 9
  • 100
  • 100
  • thanks , these are all good options , iv'e explored them all before ' maybe i should of added that in my post i thought of ChangePropertyAction but it could only be done on Dependency properties if i am not mistaken . – eran otzap Mar 22 '13 at 05:21
  • @eranotzap ChangePropertyAction works - , so if the setter just does RaisePropertyChanged that works also. – Phil Mar 22 '13 at 07:33