viewmodel属性上的PropertyChanged事件

时间:2013-03-21 18:29:17

标签: wpf blend interaction eventtrigger

我正在尝试在ViewModel中的Property上引发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" 

随时为您服务,谢谢。

1 个答案:

答案 0 :(得分:2)

您可以使用普通命令或Expression Blend的CallMethodActionInvokeCommandActionChangePropertyAction

以下是四种方法:

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

这里我使用的是MVVM Light的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; }
}