我有一个行为是为了与普通的 MVVM 一起工作而创建的,我想要一些关于如何使它与 Caliburn.Micro 一起工作的指导。
我的行为如下:
public class DropEventBehavior : Behavior<DragSource>
{
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.Drop += AssociatedObject_Drop;
}
protected override void OnDetaching()
{
this.AssociatedObject.Drop -= AssociatedObject_Drop;
base.OnDetaching();
}
void AssociatedObject_Drop(object sender, DropEventArgs e)
{
if (DropEventCommand != null)
DropEventCommand.Execute(e);
}
#region Command
public ICommand DropEventCommand
{
get { return (ICommand)GetValue(DropEventCommandProperty); }
set { SetValue(DropEventCommandProperty, value); }
}
// Using a DependencyProperty as the backing store for DropEventCommand. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DropEventCommandProperty =
DependencyProperty.Register("DropEventCommand", typeof(ICommand), typeof (DropEventBehavior), new PropertyMetadata(null));
#endregion
}
这是中继命令:
public class RelayCommand<T> : ICommand
{
#region Declarations
readonly Predicate<T> _canExecute;
readonly Action<T> _execute;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand<T>"/> class and the command can always be executed.
/// </summary>
/// <param name="execute">The execution logic.</param>
public RelayCommand(Action<T> execute)
: this(execute, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand<T>"/> class.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<T> execute, Predicate<T> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Members
public event EventHandler CanExecuteChanged;
public virtual Boolean CanExecute(Object parameter)
{
return _canExecute == null ? true : _canExecute((T)parameter);
}
public virtual void Execute(Object parameter)
{
_execute((T)parameter);
}
#endregion
}
这是 XAML:
<Grid.Resources>
<local:MainPageViewModel x:Key="MainPageViewModel"/>
</Grid.Resources>
<ListBox Name="listBox1" ItemsSource="{Binding Source1}">
<ListBox.ItemTemplate>
<DataTemplate>
<sdk:Label Content="{Binding Name}">
<ig:DragDropManager.DragSource>
<ig:DragSource IsDraggable="True">
<i:Interaction.Behaviors>
<local:DropEventBehavior
DropEventCommand="{Binding Source={StaticResource MainPageViewModel}, Path=DropCommand}"/>
</i:Interaction.Behaviors>
</ig:DragSource>
</ig:DragDropManager.DragSource>
</sdk:Label>
</DataTemplate>
</ListBox.ItemTemplate>
<ig:DragDropManager.DropTarget>
<ig:DropTarget IsDropTarget="True"/>
</ig:DragDropManager.DropTarget>
</ListBox>
这是在视图模型中处理命令的方式:
public class DragDropEventCommands
{
public ICommand DropCommand
{
get
{
return new RelayCommand<DragDropEventArgs>(
new Action<DragDropEventArgs>(
(e) =>
{
Data dragData = (e.DragSource as Label).DataContext as Data;
ObservableCollection<Data> dropSource = (e.DropTarget as ListBox).ItemsSource as ObservableCollection<Data>;
if (dragData != null && dropSource != null)
dropSource.Add(dragData);
}));
}
}
}
我想了解以下内容:
<i:Interaction.Behaviors>
<local:DropEventBehavior cal:Message.Attach="[Event DropEventCommand] = [Action HandleDropEvents($source, $eventArgs)]"/>
</i:Interaction.Behaviors>
我不反对使用触发器。在这种情况下没有成功,因为 DropEvent 不是路由事件。
如果有人能让我弄清楚这一点,我将不胜感激。
谢谢 :-)