ListView Grow instead of Scroll
Is there a way (without codebehind) to make a WPF ListView grow to the width or height of its contents rather than scroll? Sort of like a StackPanel only still selectable.
For instance, if I have:
<ScrollViewer>
<StackPanel>
<ListView ItemsSource="{Binding Rail1}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
<ListView ItemsSource="{Binding Rail2}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Ho开发者_如何学Gorizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</StackPanel>
</ScrollViewer>
the ScrollViewer does not show a horizontal scroll bar, instead the ListViews do.
It depends on container. Add ListView
itself to the ScrollViewer
- it should do the trick.
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<StackPanel>
<ListView ItemsSource="{Binding Rail1}" >
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
<ListView ItemsSource="{Binding Rail2}" >
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</StackPanel>
</ScrollViewer>
It depends on it's container, but if you set the HorizontalAlignment
property to "Stretch"
and the ListView's container will allow it, it should size itself to its content.
* Edit ** If you want both ListView's not to scroll then do something like:
<ScrollViewer>
<DockPanel>
<ListView ItemsSource="{Binding Rail1}" DockPanel.Dock="Top" />
<ListView ItemsSource="{Binding Rail2}" DockPanel.Dock="Top" />
</DockPanel>
</ScrollViewer>
I think this will give you what you want.
精彩评论