2012-07-20 53 views
2

我正在努力將ContentPresenter放入工具欄中。我有一個UserControl,DashboardView,帶有視圖模型DashboardViewModel。我ContentPresenter設置像這樣:在UserControl.Resources如何將ContentPresenter放入工具欄中

,我有:

<DataTemplate DataType="{x:Type DashboardVM:DashboardViewModel}"> 
    <Dashboard:DashboardView /> 
</DataTemplate> 

,並在工具欄上:

<ToolBarTray Margin="0" DockPanel.Dock="Top"> 
    <ToolBar Band="0" BanIndex="0"> 
     <--! other stuff --> 
    <ToolBar Band="0" BandIndex="1" MinWidth="500" ToolBarTray.IsLocked="True"> 
      <ContentPresenter Content="{Binding Path=DashboardViewModel}" /> 
    </ToolBar> 
</ToolBarTray> 

執行時,ContentPresenter不會出現。另一個工具欄。

我把ContentPresenter放在網格工具欄的外面,看起來很好。所以這是ToolBar的東西,但我無法弄清楚什麼。

更新:我也試圖(其中很多事情)把ContentPresenter的菜單項,如:

  <ToolBar Band="0" BandIndex="1"> 
       <MenuItem> 
        <MenuItem.Header> 
         <ContentPresenter Content="{Binding Path=DashboardViewModel}"/> 
        </MenuItem.Header> 
       </MenuItem> 
      </ToolBar> 

仍然沒有顯示出來。

更多信息:

DashboardView:

<UserControl x:Class="Wsi.Common.View.Dashboard.DashboardView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:Dashboard="clr-namespace:Wsi.Common.ViewModel.Dashboard" 
     xmlns:view="clr-namespace:Wsi.Common.View.Dashboard" 
     MinWidth="500" 
     MinHeight="30" 
     MaxHeight="70"> 

<UserControl.Resources> 
    <DataTemplate DataType="{x:Type Dashboard:DashboardItemViewModel}"> 
     <view:DashboardItemView /> 
    </DataTemplate> 
</UserControl.Resources> 

<StackPanel MaxHeight="70" Orientation="Horizontal"> 
    <ContentPresenter x:Name="fileSystemDashboardItem" Content="{Binding Path=FileSystemDashboardItemViewModel}" /> 
    <ContentPresenter x:Name="spreadHealthDashboardItem" Content="{Binding Path=SpreadHealthDashboardItemViewModel}" /> 
    <ContentPresenter x:Name="spreadStatsDashboardItem" Content="{Binding Path=SpreadStatsDashboardItemViewModel}" /> 
    <ContentPresenter x:Name="acquisitionStatsDashboardItem" Content="{Binding Path=AcquisitionStatsDashboardItemViewModel}" /> 
    <ContentPresenter x:Name="backhaulHealthDashboardItem" Content="{Binding Path=BackhaulHealthDashboardItemViewModel}" /> 
    <ContentPresenter x:Name="serverHealthDashboardItem" Content="{Binding Path=ServerHealthDashboardItemViewModel}" /> 
</StackPanel> 

DashboardViewModel簡單地持有性能上面子的ViewModels,只是{獲得;設置;}

如前所述,這在工具欄上方的視圖中自行工作得很漂亮。

TIA!

Janene

回答

0

回答我自己的問題......我的代碼沒有錯。 ContentPresenter的內容中的項目超出了工具欄的寬度。由於內容太寬,工具欄不顯示任何內容。

整整一天追逐一個bug,只需要我使我的**變得更小,更聰明就調整大小。

0

我想你可能有ContentControl會混淆一個ContentPresenter

A ContentPresenter用於指示視覺層次結構中自定義內容應該出現在ContentControl的ControlTemplate中的什麼位置。

A ContentControl是一個可以放入UI的實際控件。

如果你這樣做會發生什麼?

<DataTemplate DataType="{x:Type DashboardVM:DashboardViewModel}"> 
    <Dashboard:DashboardView /> 
</DataTemplate> 

<ToolBarTray Margin="0" DockPanel.Dock="Top"> 
    <ToolBar Band="0" BanIndex="0"> 
     <--! other stuff --> 
    <ToolBar Band="0" BandIndex="1" MinWidth="500" ToolBarTray.IsLocked="True"> 
      <ContentControl Content="{Binding Path=DashboardViewModel}" /> 
    </ToolBar> 
</ToolBarTray> 
+0

ContentControl給了我相同的結果。 我在一個獨立的窗口中使用了這個確切的構造,它的工作非常漂亮。 「如果存在與Content類型關聯的DataTemplate,那麼ContentPresenter將該DataTemplate應用於Content屬性,並顯示所生成的UIElement及其子元素(如果有)。「 在此基礎上從MSDN,我百思不得其解:?? – 2012-07-20 22:39:23

+0

也許這是你的'DashboardViewModel'或'DashboardView'一個問題,你可以編輯你的問題,並添加這些 – devuxer 2012-07-20 22:41:39

+0

我添加DashboardView 謝謝您 – 2012-07-20 22:57:46