如何将工具栏添加到 WPF 中列表框内部的底部、顶部、左侧或右侧?

发布于 2024-08-05 19:58:10 字数 250 浏览 2 评论 0原文

我有一个具有圆角样式的列表框。我想在属于该特定列表框的列表框中添加一个工具栏。目前,如果我在包含列表框的网格内添加一个工具栏,它将与该行中的最后一项重叠(取决于工具栏的高度)。有谁对实现这一点的最佳方法有任何想法吗?我知道我可以创建一个与列表框边缘的外观相匹配的边框控件,然后在主边框内放置一个无边框样式的列表框,并与底部的工具栏堆叠在一起,但我希望有更好的方法保持我当前的列表框样式,只需在列表框底部放置一个工具栏,不隐藏任何列表框项目。

谢谢,

约翰

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枫以 2024-08-12 19:58:11

不确定我是否完全遵循,但我认为您有几个选择:

  1. ToolBar 集成到 ListBox 模板中,可能通过编写扩展 ListBox< 的控件来实现/code> 并添加一个属性来设置 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>

在这两种情况下,结果看起来都很相似:

< a href="http://img42.imageshack.us/img42/372/screenshotof.png" rel="nofollow noreferrer">替代文本 http://img42.imageshack.us/img42/372/screenshotof.png

Not sure I follow entirely, but I think you have a couple of options:

  1. Integrate the ToolBar into the ListBox template, probably by writing a control that extends ListBox and adds a property to set the ToolBar items.
  2. Turn off the Border on the ListBox and stick your own Border around it that also encompasses the ToolBar.

2 is a little easier and is probably what you want.

Example of 1

(I didn't bother subclassing ListBox here - I just hard-coded some ToolBar items instead)

    <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

<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

╭⌒浅淡时光〆 2024-08-12 19:58:11

如果事情在视觉上重叠,很可能您的代码中声明了错误。如果您不希望它们重叠,则应该使用 Grid.Row="0" 声明 ListBox,并将工具栏声明为 Grid.Row="1" (或类似的内容)。 这篇 MSDN 文章很好地解释了网格布局。

如果您的 ListBoxItems 不是数据绑定的,您只需添加具有自定义样式的 ListBoxItem 作为列表中的最后一项。否则,您可以使用 DataTemplateSelector 进行格式化您的项目样式基于其中绑定的内容。

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. This MSDN article explains grid layouts well.

If your ListBoxItems are not databound, you can just add a ListBoxItem with a custom style as the last item in your list. Otherwise you can use a DataTemplateSelector to format your item styles based on the content bound within.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文