2013-03-21 59 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

相关问题