2009-11-19 36 views
1

我想在WPF應用程序中承載工作流設計器。 WorkflowView控件位於WindowsFormsHost控件下。我設法將工作流加載到成功鏈接到PropertyGrid的設計器上,而該PropertyGrid也託管在另一個WindowsFormsHost中。不能將活動從WPF列表框拖入工作流視圖

WorkflowView workflowView = rootDesigner.GetView(ViewTechnology.Default) as WorkflowView; 
window.WorkflowViewHost.Child = workflowView; 

大部分重新託管代碼的相同的http://msdn.microsoft.com/en-us/library/aa480213.aspx

我創建了一個自定義工具箱,它使用綁定到ToolboxItems列表的ListBox WPF控件。

<ListBox Grid.Row="1" Margin="0 0 0 4" BorderThickness="1" BorderBrush="DarkGray" ItemsSource="{Binding Path=ToolboxItems}" PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown" AllowDrop="True"> 
<ListBox.Resources> 
    <vw:BitmapSourceTypeConverter x:Key="BitmapSourceConverter" /> 
</ListBox.Resources> 
<ListBox.ItemTemplate> 
    <DataTemplate DataType="{x:Type dd:ToolboxItem}"> 
    <StackPanel Orientation="Horizontal" Margin="3"> 
    <Image Source="{Binding Path=Bitmap, Converter={StaticResource BitmapSourceConverter}}" Height="16" Width="16" Margin="0 0 3 0"  /> 
    <TextBlock Text="{Binding Path=DisplayName}" FontSize="14" Height="16" VerticalAlignment="Center" /> 
    <StackPanel.ToolTip> 
    <TextBlock Text="{Binding Path=Description}" /> 
    </StackPanel.ToolTip> 
    </StackPanel> 
    </DataTemplate> 
</ListBox.ItemTemplate> 
</ListBox> 

在ListBox_PreviewMouseLeftButtonDown處理程序:

private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
ListBox parent = (ListBox)sender; 

UIElement dataContainer; 
//get the ToolboxItem for the selected item 
object data = GetObjectDataFromPoint(parent, e.GetPosition(parent), out dataContainer); 

//if the data is not null then start the drag drop operation 
if (data != null) 
{ 
    DataObject dataObject = new DataObject(); 
    dataObject.SetData(typeof(ToolboxItem), data); 

    DragDrop.DoDragDrop(parent, dataObject, DragDropEffects.Move | DragDropEffects.Copy); 
} 
} 

使用這個配置,我無法從我的自定義工具箱拖動任何項目到設計。設計器上的光標始終顯示爲「否」。

我一直試圖在網上找到關於此事半天的事情,我真的希望有人能幫助我。

任何反饋是非常感謝。謝謝!

Carlos

回答

0

終於得到拖放工作。有三件事需要做,無論出於什麼原因WorkflowView有:

1)當做DragDrop時序列化ToolboxItem時,我不得不使用System.Windows.Forms.DataObject而不是System.Windows.DataObject。

private void ListBox_MouseDownHandler(object sender, MouseButtonEventArgs e) 
{ 
    ListBox parent = (ListBox)sender; 

    //get the object source for the selected item 
    object data = GetObjectDataFromPoint(parent, e.GetPosition(parent)); 

    //if the data is not null then start the drag drop operation 
    if (data != null) 
    { 
     System.Windows.Forms.DataObject dataObject = new System.Windows.Forms.DataObject(); 
     dataObject.SetData(typeof(ToolboxItem), data as ToolboxItem); 
     DragDrop.DoDragDrop(this, dataObject, DragDropEffects.Move | DragDropEffects.Copy); 
    } 
} 

2.)DragDrop.DoDragDrop源必須設置爲IDesignerHost中設置的IToolboxService。持有ListBox的控件實現IToolboxService。

// "this" points to ListBox's parent which implements IToolboxService. 
DragDrop.DoDragDrop(this, dataObject, DragDropEffects.Move | DragDropEffects.Copy); 

3)列表框可以綁定到下面的輔助方法返回ToolboxItems的列表,通過它的活動類型的工具框顯示:

... 
this.ToolboxItems = new ToolboxItem[] 
    { 
     GetToolboxItem(typeof(IfElseActivity)) 
    }; 
... 

internal static ToolboxItem GetToolboxItem(Type toolType) 
{ 
    if (toolType == null) 
     throw new ArgumentNullException("toolType"); 

    ToolboxItem item = null; 
    if ((toolType.IsPublic || toolType.IsNestedPublic) && typeof(IComponent).IsAssignableFrom(toolType) && !toolType.IsAbstract) 
    { 
     ToolboxItemAttribute toolboxItemAttribute = (ToolboxItemAttribute)TypeDescriptor.GetAttributes(toolType)[typeof(ToolboxItemAttribute)]; 
     if (toolboxItemAttribute != null && !toolboxItemAttribute.IsDefaultAttribute()) 
     { 
      Type itemType = toolboxItemAttribute.ToolboxItemType; 
      if (itemType != null) 
      { 
       // First, try to find a constructor with Type as a parameter. If that 
       // fails, try the default constructor. 
       ConstructorInfo ctor = itemType.GetConstructor(new Type[] { typeof(Type) }); 
       if (ctor != null) 
       { 
        item = (ToolboxItem)ctor.Invoke(new object[] { toolType }); 
       } 
       else 
       { 
        ctor = itemType.GetConstructor(new Type[0]); 
        if (ctor != null) 
        { 
         item = (ToolboxItem)ctor.Invoke(new object[0]); 
         item.Initialize(toolType); 
        } 
       } 
      } 
     } 
     else if (!toolboxItemAttribute.Equals(ToolboxItemAttribute.None)) 
     { 
      item = new ToolboxItem(toolType); 
     } 
    } 
    else if (typeof(ToolboxItem).IsAssignableFrom(toolType)) 
    { 
     // if the type *is* a toolboxitem, just create it.. 
     // 
     try 
     { 
      item = (ToolboxItem)Activator.CreateInstance(toolType, true); 
     } 
     catch 
     { 
     } 
    } 

    return item; 
} 

GetToolboxItem方法來自ToolboxService類的http://msdn.microsoft.com/en-us/library/aa480213.aspx源。

乾杯, 卡洛斯

0

這可能聽起來很愚蠢,因爲我的系統正在關閉。 :)但是,你可以檢查你的WorkflowView是否AllowDrop設置?你是否處理了DragEnter事件?

+0

是的,我已經在WindowsFormsHost和WorkflowView控制檢查的AllowDrop都和他們都設置爲true。我還處理了DragEvent,而e.Data參數字段包含了我在調用DragDrop.DoDragDrop之前所做System.Windows.DataObject的System.Windows.Forms.DataObject等價物。我能夠從e.Data中獲得ToolboxItem。 – ca7l0s 2009-11-19 09:39:36