2013-10-16 37 views
2

我正在爲我們的WPF應用程序構建UI元素,該元素允許用戶可視化以網格格式對齊的圖形集合。據我所知,你可以使用帶有WrapPanel的ItemsControl,它可以很好地將網格格式的UI元素對齊。在WPF中綁定包含WindowsFormHost項目的ItemsControl

當我們嘗試使用winforms圖形庫(zedgraph)生成圖時,就會遇到困難。這意味着我們必須使用WindowsFormsHost來顯示圖形視圖。我曾嘗試使用正常的數據綁定圖表集綁定,它並沒有真正的工作:

XAML:

<ItemsControl ItemsSource="{Binding Graphs}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <WrapPanel /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <WindowsFormsHost Margin="5"> 
       <ui:Graph></ui:Graph> 
      </WindowsFormsHost> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

上面的代碼顯示任何內容,但圖形吸氣訪問。

而且,即使我取消綁定,我只是插入的ItemsControl內一些隨機的圖形視圖...它仍然沒有任何顯示:

XAML:

<ItemsControl> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <WrapPanel /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <WindowsFormsHost Margin="5"> 
     <ui:Graph></ui:Graph> 
    </WindowsFormsHost> 
    <WindowsFormsHost Margin="5"> 
    </WindowsFormsHost> 
     <ui:Graph></ui:Graph> 
    </WindowsFormsHost> 
    <WindowsFormsHost Margin="5"> 
     <ui:Graph></ui:Graph> 
    </WindowsFormsHost> 
</ItemsControl> 

要測試確保圖形庫功能正常,這確實顯示圖表:

<Grid> 
    <WindowsFormsHost Margin="5"> 
     <ui:Graph></ui:Graph> 
    </WindowsFormsHost> 
</Grid> 

任何人都可以指引我在正確的方向嗎?非常感激。

+0

我強烈建議創建一個'UserControl'託管您的WinForms的東西,創造DependencyProperties在這個用戶控件,這樣你可以得到的綁定功能。 –

+0

感謝您的建議。任何想法爲什麼非綁定版本不顯示?圖表很好地顯示在ItemsControl外,但不在ItemsControl內。 – Vlad

+0

我想這可能與佈局有關。 winforms是可怕的,並且要求你硬編碼一切的大小。嘗試給winformshost一個固定的硬編碼「寬度」和「高度」。 –

回答

2

這可能與佈局有關。 winforms是可怕的,並且要求你硬編碼一切的大小。

嘗試給出一個固定的硬編碼的寬度和高度的winformshost

+0

確認這也適用於獲取WindowsFormsHost在WrapPanel中播放很好。然而,在那裏託管一個外部應用程序有顯示問題:/ –