通知属性修改
using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Runtime.Serialization;
namespace Microsoft.Practices.Prism.ViewModel
{
[DataContract]
public abstract class NotificationObject : INotifyPropertyChanged
{
protected NotificationObject();
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression);
protected void RaisePropertyChanged(params string[] propertyNames);
protected virtual void RaisePropertyChanged(string propertyName);
}
}
Command 注册类
using System;
namespace Microsoft.Practices.Prism.Commands
{
public class DelegateCommand<T> : DelegateCommandBase
{
public DelegateCommand(Action<T> executeMethod);
public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod);
public bool CanExecute(T parameter);
public void Execute(T parameter);
}
}
添加触发事件
添加一个触发事件类
using System;
using System.Net;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace DTO.SL
{
/// <summary>
/// Trigger触发的事件类
/// </summary>
public class ExecuteCommandAction : TriggerAction<FrameworkElement>
{
public static readonly DependencyProperty CommandNameProperty =
DependencyProperty.Register("CommandName", typeof(string), typeof(ExecuteCommandAction), null);
public string CommandName
{
get { return (string)GetValue(CommandNameProperty); }
set { SetValue(CommandNameProperty, value); }
}
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.Register("CommandParameter", typeof(object), typeof(ExecuteCommandAction), null);
public object CommandParameter
{
get { return GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
private static bool IsCommandProperty(PropertyInfo property)
{
return typeof(ICommand).IsAssignableFrom(property.PropertyType);
}
/// <summary>
/// 重写Invoke方法
/// </summary>
/// <param name="parameter"></param>
protected override void Invoke(object parameter)
{
if (AssociatedObject == null)
{
return;
}
ICommand command = null;
var dataContext = AssociatedObject.DataContext;
foreach (var item in dataContext.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (IsCommandProperty(item) && string.Equals(item.Name, CommandName, StringComparison.Ordinal))
{
command = (ICommand)item.GetValue(dataContext, null);
}
}
if ((command != null) && command.CanExecute(CommandParameter))
{
command.Execute(CommandParameter);
}
}
}
}
Xaml代码
xmlns:Custom="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:myConvert="clr-namespace:DTO.SL;assembly=DTO.SL"
<Button Width="100" Height="50" Content="大" >
<Custom:Interaction.Triggers>
<Custom:EventTrigger EventName="Click">
<myConvert:ExecuteCommandAction CommandName="MaxCommand"></myConvert:ExecuteCommandAction>
</Custom:EventTrigger>
</Custom:Interaction.Triggers>
</Button>
CS代码
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
private ICommand maxCommand;
public ICommand MaxCommand
{
get { return maxCommand; }
set { maxCommand = value; }
}
MaxCommand = new DelegateCommand(() =>
{
//事件内容
});