0

我在我的自定义序列活动中使用 WorkflowItemsPresenter,我想在那里禁用活动的复制和粘贴。我找到了一些使用 ICompositeView 接口的解决方案,我试图将其应用于 WorkflowItemsPresenter 但它不起作用。有谁知道如何在 WorkflowItemsPresenter 中禁用复制粘贴行为?

谢谢你。

这是我的疯狂代码

BranchSequenceDesigner.xaml

<sap:ActivityDesigner x:Class="MyActivities.BranchSequenceDesigner"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"
    xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation"
    >
    <Canvas Name="ContentCanvas" Width="600" Height="600"/>
    <sap:ActivityDesigner.Template>
        <ControlTemplate >
            <sap:WorkflowItemsPresenter Name="test" Items="{Binding Path=ModelItem.Children}">
                <sap:WorkflowItemsPresenter.SpacerTemplate>
                    <DataTemplate>
                        <Label HorizontalAlignment="Center" Content="Drop MyActivity Here !!!!!" FontStyle="Italic" Foreground="Red" />
                    </DataTemplate>
                </sap:WorkflowItemsPresenter.SpacerTemplate>
            </sap:WorkflowItemsPresenter>
        </ControlTemplate>
    </sap:ActivityDesigner.Template>

</sap:ActivityDesigner>

BranchSequenceDesigner.xaml.cs

using System;
using System.Collections.Generic;
using System.Windows;
using System.Activities.Presentation;
using System.Activities.Presentation.Model;
using System.Activities;
using System.Activities.Presentation.Services;

namespace MyActivities
{
    public partial class BranchSequenceDesigner : ICompositeView
    {
        public BranchSequenceDesigner()
        {
            InitializeComponent();
        }

        protected override void OnModelItemChanged(object newValue)
        {
            ModelItem canvasActivity = (ModelItem)newValue;
            Update(canvasActivity);
            canvasActivity.Properties["Children"].Collection.CollectionChanged +=
                new System.Collections.Specialized.NotifyCollectionChangedEventHandler
                   ((senders, args) => Update(this.ModelItem));
        }

        void Update(ModelItem canvasActivity)
        {

            this.ContentCanvas.Children.Clear();
            foreach (ModelItem modelItem in canvasActivity.Properties["Children"].Collection)
            {
                var view = Context.Services.GetService<ViewService>().GetView(modelItem);
                this.ContentCanvas.Children.Add((UIElement)view);
                DragDropHelper.SetCompositeView((WorkflowViewElement)view, this);
            }
        }

        protected override void OnPreviewDragEnter(DragEventArgs e)
        {
            if (DragDropHelper.AllowDrop(

                    e.Data,
                    this.Context,
                    typeof(Activity)))
            {
                e.Effects = (DragDropEffects.Move & e.AllowedEffects);
                e.Handled = true;
            }

            base.OnDragEnter(e);
        }

        protected override void OnPreviewDragOver(DragEventArgs e)
        {
            if (DragDropHelper.AllowDrop(

                    e.Data,
                    this.Context,
                    typeof(Activity)))
            {
                e.Effects = (DragDropEffects.Move & e.AllowedEffects);
                e.Handled = true;
            }

            base.OnDragOver(e);
        }

        protected override void OnPreviewDrop(DragEventArgs e)
        {
            ModelItem canvasActivity = this.ModelItem;
            object droppedItem = DragDropHelper.GetDroppedObject(this, e, this.Context);
            using (ModelEditingScope scope = canvasActivity.BeginEdit())
            {
                ModelItem droppedModelItem = canvasActivity.Properties["Children"].Collection.Add(droppedItem);
                scope.Complete();
            }
            e.Handled = true;
            DragDropHelper.SetDragDropCompletedEffects(e, DragDropEffects.Move);
            base.OnDrop(e);
        }


        bool ICompositeView.CanPasteItems(List<object> itemsToPaste)
        {
            return false;
        }

        System.Activities.Presentation.View.TypeResolvingOptions ICompositeView.DroppingTypeResolvingOptions
        {
            get { throw new NotImplementedException(); }
        }

        bool ICompositeView.IsDefaultContainer
        {
            get { throw new NotImplementedException(); }
        }

        void ICompositeView.OnItemMoved(System.Activities.Presentation.Model.ModelItem modelItem)
        {
            throw new NotImplementedException();
        }

        object ICompositeView.OnItemsCopied(List<System.Activities.Presentation.Model.ModelItem> itemsToCopy)
        {
            throw new NotImplementedException();
        }

        object ICompositeView.OnItemsCut(List<System.Activities.Presentation.Model.ModelItem> itemsToCut)
        {
            throw new NotImplementedException();
        }

        void ICompositeView.OnItemsDelete(List<System.Activities.Presentation.Model.ModelItem> itemsToDelete)
        {
        }

        void ICompositeView.OnItemsPasted(List<object> itemsToPaste, List<object> metadata, Point pastePoint, WorkflowViewElement pastePointReference)
        {
            throw new NotImplementedException();
        }
    }
}

分支序列.cs

using System.Activities;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Markup;

namespace MyActivities
{
    [Designer(typeof(BranchSequenceDesigner))]
    [ContentProperty("Children")]
    public sealed class BranchSequence : NativeActivity
    {
        [DefaultValue(null)]
        private Collection<Activity> _children;
        public Collection<Activity> Children {
            get
            {
                return (_children = _children ?? new Collection<Activity>());
            }
        }

        protected override void Execute(NativeActivityContext context) {}
    }
}
4

1 回答 1

1

我通过使用 DesignerView 中的 CommandBinding 解决了我的复制粘贴问题。这是一个有用的链接

using System.Activities.Presentation.View;
using System.Windows;
using System.Windows.Input;

namespace MyActivities
{
    public partial class BranchSequenceDesigner
    {
        public BranchSequenceDesigner()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(this.BaseWorkflowDesigner_Loaded);
        }

        private void BaseWorkflowDesigner_Loaded(object sender, RoutedEventArgs e)
        {

            foreach (CommandBinding cb in this.Designer.CommandBindings)
            {
                if (cb.Command.Equals(ApplicationCommands.Delete))
                {
                    cb.CanExecute += new CanExecuteRoutedEventHandler(this.OnDeleteCommandCanExecute);
                }
                else if (cb.Command == DesignerView.CopyCommand)
                {
                    cb.CanExecute += new CanExecuteRoutedEventHandler(this.OnCopyCommandCanExecute);
                }
                else if (cb.Command == DesignerView.PasteCommand)
                {
                    cb.CanExecute += new CanExecuteRoutedEventHandler(this.OnPasteCommandCanExecute);
                }
                else if (cb.Command == DesignerView.CutCommand)
                {
                    cb.CanExecute += new CanExecuteRoutedEventHandler(this.OnCutCommandCanExecute);
                }
            }
        }

        private void OnDeleteCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
        }

        private void OnCutCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
        }

        private void OnCopyCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
        }

        private void OnPasteCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
        }
    }
}
于 2012-11-07T14:56:47.683 回答