WPF 将标头添加到 ListBox,使其像 DataGrid 一样滚动
我正在尝试创建一个使用 ListBox
和我的自定义标头的布局,该标头看起来像标尺,但用于日期(具有明确的开始和结束日期)。目标是具有类似于 DataGrid 的外观和感觉,只不过列标题行将被我的 DateTape 对象替换。当用户水平滚动时,DateTape
和 ListBox
都会滚动,但当用户垂直滚动时,只有 ListBox
滚动,DateTape
位于顶部(就像 DataGrid
中的列标题行)。
到目前为止,我能做的最好的事情如下:
<Window x:Class="ProjectNS.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:ProjectNS"
Title="MainWindow" Height="350" Width="600">
<Window.Resources>
<DataTemplate x:Key="itemTemplate">
<my:CustomRectangle HorizontalAlignment="Left" VerticalAlignment="Top" />
</DataTemplate>
</Window.Resources>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="File" />
</Menu>
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<DockPanel>
<my:DateTape DockPanel.Dock="Top" VerticalAlignment="Top">
<my:DateTape.Dates>
<CalendarDateRange Start="10/4/2011" End="11/4/2011" />
</my:DateTape.Dates>
</my:DateTape>
<ListBox ItemTemplate="{StaticResource itemTemplate}" />
</DockPanel>
</ScrollViewer>
</DockPanel>
</Window>
此解决方案的唯一问题是 ListBox
的垂直滚动条位于控件的最右侧,这意味着用户必须水平滚动才能显示滚动条。我需要滚动条始终可见。
我尝试将 DateTape
和 ListBox
放入 ScrollViewer
中,但垂直滚动时 DateTape
滚动到视图之外。
仅供参考 - 我的 CustomRectangle
对象是一个 UserControl
,允许用户实时调整水平位置和宽度,以根据需要将其定位为与 DateTape< /代码>。
I'm trying to create a layout that uses a ListBox
and my custom header that looks like a ruler, but for dates (with explicit start and end dates). The goal is to have an appearance and feel similar to a DataGrid
, except that the column header row would be replaced by my DateTape
object. When the user scrolls horizontally, the DateTape
and ListBox
both scroll, but when the user scrolls vertically, only the ListBox
scrolls and the DateTape
stays at the top (like the column header row in the DataGrid
).
So far, the best I've been able to do is as follows:
<Window x:Class="ProjectNS.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:ProjectNS"
Title="MainWindow" Height="350" Width="600">
<Window.Resources>
<DataTemplate x:Key="itemTemplate">
<my:CustomRectangle HorizontalAlignment="Left" VerticalAlignment="Top" />
</DataTemplate>
</Window.Resources>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="File" />
</Menu>
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<DockPanel>
<my:DateTape DockPanel.Dock="Top" VerticalAlignment="Top">
<my:DateTape.Dates>
<CalendarDateRange Start="10/4/2011" End="11/4/2011" />
</my:DateTape.Dates>
</my:DateTape>
<ListBox ItemTemplate="{StaticResource itemTemplate}" />
</DockPanel>
</ScrollViewer>
</DockPanel>
</Window>
The only problem I have with this solution is that the vertical scrollbar for the ListBox
is at the extreme right of the control which means the user has to scroll horizontally to make the scrollbar appear. I need the scrollbar visible at all times.
I tried placing the DateTape
and ListBox
into a ScrollViewer
, but then the DateTape
scrolls out of view when scrolling vertically.
FYI - My CustomRectangle
object is a UserControl
that allows the user to adjust the horizontal position and width real-time to position it as desired in line with the DateTape
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终不得不进行一些重组。
ListBox
现在是一个嵌套在ScrollViewer
中的ItemsControl
,其中垂直滚动条被隐藏(未禁用)。我还有一个独立的ScrollBar
停靠在右侧,它与代码隐藏中的ScrollViewer
中的垂直滚动条相关联。这处理垂直滚动。最后,辅助ScrollViewer
包含设置为处理水平滚动的DateTape
和ItemsControl
。XAML
C#
我尝试通过使用
ItemsPanelTemplate
将独立的ScrollBar
绑定到ItemsControl
中的滚动条,但我无法做到这一点去工作。I ended up having to restructure a bit. The
ListBox
is now anItemsControl
nested within aScrollViewer
with the vertical scroll bar hidden (not disabled). I also have an independentScrollBar
docked to the right side that is tied to the vertical scroll bar in theScrollViewer
in the code-behind. This handles the vertical scrolling. Finally, a secondaryScrollViewer
contains theDateTape
and theItemsControl
set to handle the horizontal scrolling.XAML
C#
I tried to tie the independent
ScrollBar
to the scroll bar in theItemsControl
through use of aItemsPanelTemplate
, but I couldn't get that to work.