2013-03-21 77 views
-3

我想使用交互觸發器在我的ViewModel 屬性上引發PropertyChanged事件。ViewModel屬性PropertyChanged事件

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> 
當然

如果有這個問題,您在您的處置有引用

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

有任何疑問,在此先感謝。

+0

你爲什麼要這麼做?您正在有效地將邏輯添加到您的視圖中。爲什麼你不能將按鈕命令綁定到ICommand,然後將你的屬性從ViewModel改爲? – 2013-03-21 18:37:24

+0

因爲邏輯是查看相關, 除了爲什麼不是問題,問題如何。 – 2013-03-21 18:40:13

+0

沒有可能需要這樣做。 – 2013-03-21 21:18:34

回答

2

您可以使用常規命令或E​​xpression Blend的CallMethodAction,InvokeCommandActionChangePropertyAction

這裏有四種方法可以做你想做的:我使用MVVM光的ViewModelBase

<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> 

這裏:

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; } 
} 
+0

謝謝,這些都是很好的選擇,iv'e在 之前探究過它們'也許我應該補充說,在我的帖子中 我想到了ChangePropertyAction,但它只能在依賴屬性上完成,如果我沒有弄錯的話。 – 2013-03-22 05:21:25

+0

@eranotzap ChangePropertyAction works - ,所以如果setter只是RaisePropertyChanged也可以。 – Phil 2013-03-22 07:33:42

相關問題