[英]How to add toolbar to bottom, top, left, or right of the inside of a listbox in WPF?
I have a listbox with a style that has rounded corners. 我有一个带有圆角样式的列表框。 I'd like to add a toolbar inside the listbox that pertains to that specific list box.
我想在与该特定列表框相关的列表框中添加工具栏。 Currently, if I add a toolbar inside the grid that contains a listbox, it will overlap the last item in the row (depending on the height of the toolbar).
当前,如果我在包含列表框的网格内添加一个工具栏,它将与行中的最后一项重叠(取决于工具栏的高度)。 Does anyone have any ideas on the best way to implement this?
是否有人对实现此目标的最佳方法有任何想法? I know I could create a border control that matches the look of the listbox edges and then place a listbox that has a style without borders inside the main border stacked with the toolbar at the bottom, but I'm hoping there is a better way to keep my current listbox style and just place a toolbar inside the bottom of the listbox that doesn't hide any listbox items.
我知道我可以创建一个与列表框边缘的外观匹配的边框控件,然后将一个具有无边框样式的列表框放置在底部带有工具栏的主边框内,但是我希望有一种更好的方法保持我当前的列表框样式,只需将工具栏放在列表框底部即可隐藏任何列表框项目。
Thanks, 谢谢,
John 约翰
Not sure I follow entirely, but I think you have a couple of options: 不确定我是否会完全遵循,但我认为您有两种选择:
ToolBar
into the ListBox
template, probably by writing a control that extends ListBox
and adds a property to set the ToolBar
items. ListBox
并添加属性以设置ToolBar
项目的ListBox
,可以将ToolBar
集成到ListBox
模板中。 Border
on the ListBox
and stick your own Border
around it that also encompasses the ToolBar
. ListBox
上的Border
,并在其周围粘贴还包含ToolBar
自己的Border
。 2 is a little easier and is probably what you want. 2稍微容易一些,可能正是您想要的。
Example of 1 示例1
(I didn't bother subclassing ListBox here - I just hard-coded some ToolBar items instead) (我没有在这里麻烦子类化ListBox-我只是硬编码了一些ToolBar项)
<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>
Example of 2 例子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>
In both cases, the result looks similar: 在这两种情况下,结果看起来都相似:
alt text http://img42.imageshack.us/img42/372/screenshotof.png 替代文字http://img42.imageshack.us/img42/372/screenshotof.png
Most likely you have something declared wrong in your code if things are being overlapped visually. 如果视觉上重叠,则很可能在代码中声明了错误。 You should have your ListBox declared with Grid.Row="0" and your toolbar as Grid.Row="1" (or something similar) if you'd like them not to overlap.
如果您不希望它们重叠,则应该使用Grid.Row =“ 0”声明ListBox并将工具栏声明为Grid.Row =“ 1”(或类似名称)。 This MSDN article explains grid layouts well.
此MSDN文章很好地解释了网格布局。
If your ListBoxItems are not databound, you can just add a ListBoxItem with a custom style as the last item in your list. 如果您的ListBoxItems没有数据绑定,则只需添加具有自定义样式的ListBoxItem作为列表中的最后一项。 Otherwise you can use a DataTemplateSelector to format your item styles based on the content bound within.
否则,您可以使用DataTemplateSelector根据其中绑定的内容来设置项目样式的格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.