我的行为是与普通的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&lt;T&gt;"/> 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&lt;T&gt;"/> 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不是路由事件。

如果有人可以解决这个问题,我将不胜感激。

谢谢 :-)

===============>>#1 票数:-2

您找到这些了,对您有帮助吗? 我确定我记得读过一些东西,但记不清。

进行了这些快速搜索以查看是否可以记住并对此进行思考。

  ask by MrTouya translate from so

未解决问题?本站智能推荐: