2009-09-23 64 views
0

我有一個帶有圓角的樣式的列表框。我想在列表框中添加一個與該特定列表框相關的工具欄。目前,如果我在包含列表框的網格中添加一個工具欄,它將重疊行中的最後一個項目(取決於工具欄的高度)。有沒有人有任何想法來實現這個最好的方法?我知道我可以創建一個與列表框邊緣相匹配的邊框控件,然後放置一個帶有無邊框樣式的列表框,並將其放在主工具欄的底部,但我希望有更好的方法保持我當前的列表框樣式,只是在列表框底部放置一個不隱藏任何列表框項目的工具欄。如何將工具欄添加到WPF中列表框的底部,頂部,左側或右側?

感謝,

約翰

回答

1

不知道我遵循完全,但我認爲你有兩個選擇:

  1. 整合ToolBarListBox模板,可能通過編寫擴展ListBox並增加了一個屬性來設置ToolBar控制項目。
  2. 關閉ListBox上的Border並在其周圍粘貼您自己的Border,該文件還包含ToolBar

2是有點更容易,也可能是你想要的。

例1

(我沒有打擾這裏ListBox的子類 - 我只是硬編碼,而不是某些工具欄項目)

<Grid Margin="10"> 
     <ListBox> 
      <ListBox.Template> 
       <ControlTemplate TargetType="ListBox"> 
        <Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="1,1,1,1" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="Bd" SnapsToDevicePixels="True" CornerRadius="5"> 
         <Grid> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="Auto"/> 
           <RowDefinition Height="*"/> 
          </Grid.RowDefinitions> 

          <ToolBarTray Background="White"> 
           <ToolBar Band="1" BandIndex="1"> 
            <Button> 
             Cut 
            </Button> 
            <Button> 
             Copy 
            </Button> 
            <Button> 
             Paste 
            </Button> 
           </ToolBar> 
           <ToolBar Band="2" BandIndex="1"> 
            <Button> 
             Undo 
            </Button> 
            <Button> 
             Redo 
            </Button> 
           </ToolBar> 
          </ToolBarTray> 
          <ScrollViewer Grid.Row="1" Padding="{TemplateBinding Control.Padding}" Focusable="False"> 
           <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" /> 
          </ScrollViewer> 
         </Grid> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="UIElement.IsEnabled" Value="False"> 
          <Setter Property="Panel.Background" TargetName="Bd"> 
           <Setter.Value> 
            <DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" /> 
           </Setter.Value> 
          </Setter> 
         </Trigger> 
         <Trigger Property="ItemsControl.IsGrouping" Value="True"> 
          <Setter Property="ScrollViewer.CanContentScroll" Value="False"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </ListBox.Template> 
      <ListBoxItem>One</ListBoxItem> 
      <ListBoxItem>Two</ListBoxItem> 
      <ListBoxItem>Three</ListBoxItem> 
     </ListBox> 
    </Grid> 

例2

<Grid Margin="10"> 
    <Border CornerRadius="5" BorderThickness="1" BorderBrush="Black" Padding="1"> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="*"/> 
      </Grid.RowDefinitions> 

      <ToolBarTray Background="White"> 
       <ToolBar Band="1" BandIndex="1"> 
        <Button> 
         Cut 
        </Button> 
        <Button> 
         Copy 
        </Button> 
        <Button> 
         Paste 
        </Button> 
       </ToolBar> 
       <ToolBar Band="2" BandIndex="1"> 
        <Button> 
         Undo 
        </Button> 
        <Button> 
         Redo 
        </Button> 
       </ToolBar> 
      </ToolBarTray> 
      <ListBox Grid.Row="1" BorderThickness="0"> 
       <ListBoxItem>One</ListBoxItem> 
       <ListBoxItem>Two</ListBoxItem> 
       <ListBoxItem>Three</ListBoxItem> 
      </ListBox> 
     </Grid> 
    </Border> 
</Grid> 

在這兩種情況下,結果都看起來類似:

alt text http://img42.imageshack.us/img42/372/screenshotof.png

0

最有可能的,你有什麼錯宣佈在你的代碼,如果事情被視覺重疊。如果你希望它們不要重疊,你應該讓你的ListBox用Grid.Row =「0」和你的工具欄作爲Grid.Row =「1」(或類似的東西)聲明。 This MSDN article很好地解釋了網格佈局。

如果您ListBoxItems沒有數據綁定,您可以只需添加一個ListBoxItem的自定義樣式在列表中的最後一項。否則,您可以使用DataTemplateSelector根據綁定的內容格式化您的項目樣式。

相關問題