不知道我遵循完全,但我认为你有两个选择:
- 整合
ToolBar
到ListBox
模板,可能通过编写扩展ListBox
并增加了一个属性来设置ToolBar
控制项目。
- 关闭
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