StackPanels going side by side - wpf

I have this small stackpanel that gathers information from a class I have. I want the stack panels to go side by side instead of just stacking up on top of each other. This is the code I have
<SplitView.Content>
<Grid>
<ListView x:Name ="View">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<StackPanel>
<TextBlock Text="{Binding title}"></TextBlock>
<TextBlock Text="{Binding location}"></TextBlock>
<TextBlock Text="{Binding date}"></TextBlock>
<TextBlock Text="{Binding desc}"></TextBlock>
<Button HorizontalAlignment="Right" FontFamily="Segoe MDL2 Assets" Content=""></Button>
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</SplitView.Content>
I don't want them to go infinitly to the side though, just like 2 next to each other and then another 2 bellow, if that is possible

You can set ListView.ItemsPanel to a StackPanel with Horizontal Orientation.
<ListView x:Name ="View">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>

Related

WPF Wrappanel in Listbox

I want to display several images in a vertical scrolling list, but these images are subdivided into several groups that must be distiguishable by the user. To achive this, I use a ListView which items contain a WrapPanel, which contains the single images:
<ListView Name="ListGroups" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Label Content="{Binding Groupname}" />
<ListBox ItemsSource="{Binding Images}" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Image Source="{Binding Thumb}" />
<Label Content="{Binding Res}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
What I get is:
Current result - wrong
but what I want to achive is:
Thats what I want
That is to say I dont want any horizontal Scollbars at all, and the groups clearly must be separated. On the other hand, when sizing the window, the images within one group must wrap to fill all available space.
Just also disable the horizontal ScrollBar of the inner ListBox, and set Stretch="None" on the Image element.
<ListView ... ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding Groupname}"/>
<ListBox ItemsSource="{Binding Images}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding Thumb}" Stretch="None"/>
<Label Content="{Binding Res}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

WPF - adding user control to listbox

I am trying to add a usercontrol to a ListBox.
My usercontrol contains a collection of basic elements like textbox and dropdowns, in a fashion so that it creates a row of elements.
The code for ListBox in my main window is as -
<GroupBox FontWeight="SemiBold" Foreground="#FF0CAEF9" Name="gbAddProducts" Style="{x:Null}" Header="ADD PRODUCTS" HorizontalAlignment="Left" Margin="0,256,0,0" VerticalAlignment="Top" Width="990" Height="207">
<ListBox Name="lstboxAddProduct" ItemsSource="{Binding Path=AddNewProductRowViewModelList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" BorderThickness="0" Margin="0,10,-2,23">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<ContentControl Content="{Binding AddNewProductRowViewModel}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<Validation.ErrorTemplate>
<ControlTemplate>
<StackPanel>
<AdornedElementPlaceholder x:Name="aepForError"/>
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ErrorContent}" Foreground="White" Background="#DC000C" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ControlTemplate>
</Validation.ErrorTemplate>
</ListBox>
</GroupBox>
Here AddNewProductRowViewModelList is my user control list containing 5 controls.
The Problem that I see is that when I run the code, the screen has 5 rows, ie I can click on the area to figure out theer are 5 rows as that section gets highlighted. But they are not visible.
Could it be some 'bring to front' sort of issue.
Please advise.
Thanks in advance.
I would do it this way instead:
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<ContentControl>
<local:AddNewProductRowView Datacontext="{Binding AddNewProductRowViewModel}"/>
</ContentControl>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>

VirtualizingStackPanel is working fine in ListView's ItemPanel. But not the simple StackPanel

I just want to display Items in ListView like windows explorer (Large icons). For the same I used the below code.
<ListView Name="lstView" ItemsSource="{Binding MovieList}" >
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<!-- <VirtualizingStackPanel/> --> <!-- Working Fine -->
<StackPanel/> <!-- Items are not displayed -->
<!-- It must be wrap panel -->
<!-- OK, lets see with simple -->
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Path}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
In the above code, If I use VirtualizingStackPanel then the items are displayed fine, but one by one which I don't want. If I use StackPanel, the items are not dispalyed in screen, even the items are not added in StackPanel. I checked with Snoop tool.
I should use WrapPanel instread of StackPanel to list items, but lets see the simple StackPanel.
Why the items are not displayed in StackPanel?
Basically I want list the items like WindowExplorer large icon view.
I am using .NET 4.0
I tried same thing in my code and it's working fine. Virtualization works as long as size of the ItemsControl's viewport is restricted.
just try replacing the code given below:
<ListView ItemsSource="{Binding Movies}" Width="400" HorizontalAlignment="Left" >
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel VirtualizingPanel.IsVirtualizing="True" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Year}" FontSize="12" Margin="10,0,0,0"/>
<TextBlock Text="{Binding Name}" FontSize="12" Margin="10,0,0,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Category}" FontSize="12" Margin="10,0,0,0"/>
<TextBlock Text="{Binding Director}" FontSize="12" Margin="10,0,0,0"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

Showing a collection of items inside of a ItemsControl horizontally

Here is the XAML markup:
<ScrollViewer Grid.Column="1" Grid.Row="2" HorizontalScrollBarVisibility="Disabled" Width="990">
<StackPanel Margin="50 0 0 40">
<ItemsControl x:Name="streamList" ItemsSource="{Binding StreamInformations}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10" Orientation="Horizontal" >
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ImageUrl}" Height="60" />
<StackPanel Margin="10 0 0 0" VerticalAlignment="Center">
<TextBlock Foreground="Black" Text="{Binding ChannelName}" FontSize="12" />
<TextBlock Foreground="#999" Text="{Binding PlayerName}" FontSize="10" />
<TextBlock Foreground="#999" Text="{Binding ViewCount}" FontSize="10" />
</StackPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
And this is how it looks:
I'd like these items to appear horizontally and flow down when it reaches the edge of the StackPanel.
It seems that currently, each item in my DataContext collection is creating it's own StackPanel, so this isn't what I want.
Any suggestions?
If you change the ItemsPanel template to either a WrapPanel or horizontal StackPanel, you can achieve the effect you are after...
<ItemsControl>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<!--other stuff here-->
</ItemsControl.ItemTemplate>
</ItemsControl>
In this snippet, the ItemsPanel is set to a WrapPanel oriented horizontally. The ItemTemplate would include the binding and styling you already have...

Order Images in ListBox DataTemplate Horizontally

Preview
alt text http://img39.imageshack.us/img39/5466/howtoorderhorizontal.jpg
On the highlighted item, the images still ordered vertically even I already use <StackPanel Orientation="Horizontal">. Am I missing something?
I don't want the images have ListBoxItem behavior (hover/click). I had added IsEnabled="False" to the list box, but the images' opacity decreased : ( Do you have any idea how to do this thing?
Data template
<!-- FacilityTreeView data template -->
<telerik:HierarchicalDataTemplate x:Key="FecilityTemplate" ItemsSource="{Binding Facilities}">
<StackPanel Orientation="Horizontal">
<ListBox ItemsSource="{Binding Icons}" BorderThickness="0" Background="Transparent">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Source}" Margin=" 0,0,2,0" ToolTipService.ToolTip="{Binding Tooltip}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock Text="{Binding Description}" VerticalAlignment="Center" />
</StackPanel>
</telerik:HierarchicalDataTemplate>
By using ItemsPanelTemplate.
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Source}" Margin=" 0,0,2,0" ToolTipService.ToolTip="{Binding Tooltip}" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ListBox.ItemsPanelTemplate>
You need to use <StackPanel Orientation="Horizontal"> as an ItemsPanelTemplate.
Read more here.
I was trying the solution and I found it's incomplete, ItemsPanelTemplate must be inside <ListBox.ItemsPanel>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>

Resources