I have a Listbox where i am binding a list of strings to it. By default the items in the List box scroll vertically. But I want these items to scroll horizontally. Here is the piece of xaml I have.
<ListBox Grid.Row="1" FontSize="25" Name="lstitems" Height="400"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="0,15,0,0"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Name="tbl" Text="{Binding}">
<LineBreak></LineBreak>
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I tried various approaches but it does not scroll horizontally. With this code I am able to move the items to the left but the rest of the items are not being loaded. The item source property is set from the cs file.
You might try to use a WrapPanel oriented vertically.
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
Also, don't forget to set ScrollViewer.HorizontalScrollBarVisibility to Auto
Related
enter image description here
Please check the image.
There is a checkbox in the template in the listbox, and I set the red background color in it.
When I click on the red checkbox works fine.
But when I click on the image black arrow, it is not checked.
How can I clear the whitespace and synchronize it with the checkbox?
I tried setting margin padding but couldn't solve it.
<ListBox x:Name="xList" Grid.Row="1" HorizontalContentAlignment="Stretch">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical" IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="red">
<CheckBox IsChecked="{Binding IsVisible, Mode=TwoWay}"
VerticalContentAlignment="Center"
Content="{Binding Header}"
Height="28" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Thank you for helping me.
Add this code to your ListBox:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="0" />
</Style>
</ListBox.ItemContainerStyle>
This will set the Padding of the ListBoxItem container to 0. The result looks like this:
I have a long page with a ScrollViewer (like web-page).
Page contains header and list of lazy loading infinite scrolling content. Header should scroll away with list content. Header height is 5 times larger than list item height.
Is there any good way to virtualize this?
<ScrollViewer>
<StackPanel>
<TextBlock Text="Header:" />
<StackPanel Orientation="Horizontal" Height="500">
<!--Complex UI-->
</StackPanel>
<TextBlock Text="Videos:"/>
<ItemsControl ItemsSource="{Binding Videos}"
ItemsTemplate="{StaticResource VideoDataTemplate}" />
</StackPanel>
</ScrollViewer>
What I've tried:
Make DataTemplateSelector for header/list item and put everything into one ItemsControl. Reason:
ScrollViewer with CanContentScroll="True" scrolls header away on first scroll tick. This is not acceptable because header is too large.
Make two ScrollViewers. One for header with large bottom margin, one for items with header height top margin. Reason: Too hard to sync top offset and mouse events.
I found a surprising solution: use of Virtualizing TreeView with HierarchicalDataTemplate.
<TreeView ItemsSource="{Binding VideosViewModel}"
ScrollViewer.VerticalScrollBarVisibility="Visible"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.IsVirtualizingWhenGrouping="True"
VirtualizingStackPanel.ScrollUnit="Pixel"
VirtualizingStackPanel.VirtualizationMode="Recycling">
<TreeView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</TreeView.ItemsPanel>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True" />
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Videos}">
<!--Header-->
<StackPanel Orientation="Horizontal" Height="500">
<!--Complex UI-->
</StackPanel>
<!--Items-->
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name, Mode=OneTime}" />
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
This solution is stable and fast.
I have a listbox using UniformGrid for the ItemsPanelTemplate. It is a list of photos. I want the photos to be centered horizontally in the center of each cell of the grid, but it seems that no matter what I do, the images are aligned to the left of each cell. Here is my current XAML:
<Border BorderThickness="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DockPanel.Dock="Right">
<ListBox Name="PhotosListBox" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid IsItemsHost="True" HorizontalAlignment="Center"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Path=photo}" HorizontalAlignment="Center"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Border>
As you can see, I have the Image control in the DataTemplate set to HorizontalAlignment="Center", which I thought would do it, but it's not working.
What am I doing wrong?
You need to set HorizontalContentAlignment to Stretch to first allow ListBoxItems to stretch to all available space so that inline control can be aligned at centre accordingly.
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
...
</ListBox>
A have listbox
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel>
<Button Content="{Binding name_trainer}" Tag="{Binding idPersonTrainer}" DockPanel.Dock="Top" HorizontalAlignment="Center">
</Button>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox HorizontalContentAlignment="Center">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding yourstuff}" FontSize="72"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel HorizontalAlignment="Center"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
The accepted answer is right, however I needed a bit more:
not only to center, but also to horizontally stretch the items. Just changing HorizontalAlignment to Stretch didn't work, so, here's how it's done:
<ItemsPanelTemplate>
<StackPanel HorizontalAlignment="Stretch">
<StackPanel.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</StackPanel.Resources>
</StackPanel>
</ItemsPanelTemplate>
You may have to center the DockPanel, rather than the data inside it. I've seen this behavior with StackPanel, because the panel shrinks to the contents. The centering ends up working properly, but in the wrong context.
I've got an ItemsControl which fills from top to bottom, but I can't get it's child items to occupy the whole width of the ItemsControl:
I basically need to stretch the green bits to fill the width of the control (as shown by the blue bits).
I've tried things like setting the HorizontalAlignment property of the template item to Stretch and I've even tried binding it's Width property to the ItemsControl Width, but neither worked.
Should be a straight forward one, but it's something I just can't quite figure out...
Edit: here's the ItemTemplate (the whole thing is the ItemTemplate which itself contains an ItemsControl which is bound to a list of child objects):
<DataTemplate>
<Border CornerRadius="5" Background="#ddd">
<StackPanel>
<TextBlock Text="{Binding Name}" FontSize="18" Foreground="#bbb"/>
<ItemsControl ItemsSource="{Binding PlugIns}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel HorizontalAlignment="Stretch" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10,0,10,10" Tag="{Binding}"
MouseEnter="StackPanel_MouseEnter">
<Border Child="{Binding Icon}" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Border>
</DataTemplate>
You need to configure the ListBoxItem HorizontalContentAlignment, you do this by using a Style object in the ListBox ItemContainerStyle as follows:-
<ListBox ....>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
OK, so as I continued to build the app up, I figured out how to resolve this. Essentially, when I changed the template of the ItemsControl to support scrolling, the items spanned to fill horizontally :)
<ItemsControl>
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer Padding="{TemplateBinding Padding}">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
...
</ItemsControl>