WPF WrapPanel/StackPanel with DataTemplate? - wpf

What must I do to use a DataTemplate in a WrapPanel or StackPanel?
On a ListBox its so easy but i cant find a way to do it on a Panel...
Edit: What i want is a ListBox that places the items like a WrapPanel.

If I understand you correctly, you can use the ItemsPanel property of your container. I used something similar to make my ItemsControl layout using a horizontal StackPanel:
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
So, more specifically for your case:
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>

There isn't a DataTemplate property for those panels..
You can however use a ControlTemplate for these panels, and set these in the Template property of these panels...
HTH

Related

Horizontal ListView on multiple lines (WPF)

I want to implement an horizontal ListView which can have multiple lines, like the file explorer:
I found that I have to use StackPanelfor the ItemPanelTemplate here, but I prefer to have multiple lines instead of the horizontal scrollbar.
I think that the idea is when the StackPanel width reaches the ListView width, go the next line/create a new StackPanel. I don't know if it's correct, but maybe it can help to understand what am I looking for.
How can I implement this?
You need to use a WrapPanel instead of a StackPanel as ItemsPanelTemplate.
<ListView ItemsSource="{Binding xxx}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
Use a WrapPanel and specify the width.
<ListView ItemsSource="your-source">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel width="1200" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>

Items Box with limit of objects in row (Large icons view style in Windows Explorer)

Can't find a solution to a pretty simple UI problem:
I have a model with a Images property. The Images property holds a collection of items Image.
As for now on - I have a ListBox and binding a ListBoxItem data template to Images.Image and all good. But I have each item on a new line. Not good.
What I am willing to achieve is, lets describe as, a Listbox with Horizontal items orientation and limit of items in a row. Just like Large icons view style in Windows Explorer.
Have somebody previously implemented such a solution? Any advice will be highly appreciated.
Thank you in advance.
Use a WrapPanel (or some other appropriate Panel) as the ListBox's ItemsPanel, and disable horizontal scrolling:
<ListBox ItemsSource="{Binding Images}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}" Width="100" Margin="5"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
You can set the ListBox's ItemPanelTemplate to WrapPanel, like this.
I am not sure why it is always like that - but as soon as I asked, I have found an alternative solution with usage of ListView:
<ListView ItemsSource="{Binding Images}">
<ListView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Image}" />
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="2" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>

WPF itemscontrol child alignment

I have a WPF MVVM application with an itemscontrol. The number of horizontal child items is influenced by the itemscontrol width. There is only 1 thing i haven't solved. I'm trying to align the child elements in a way that there are always centered. I have made quick pictures in paint to demonstrate what i mean.
How it's now:
If the width further inceares then a forth item will be added horizontally. This function needs to stay.
How I would like to see it:
If there is enough room for a forth item then it needs to be added. I'm thinking that the answer could be a simple XAML property. Anybody an idea?
Put the ItemsControl in a Grid and set its HorizontalAlignment to Center:
<Grid>
<ItemsControl ItemsSource="{Binding Images}" HorizontalAlignment="Center">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}" .../>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>

ListView with Rows and Columns

I have a ListView in WPF. I have an ObservableCollection as the ItemsSource. I want the items to simply flow from left to right and then onto the next row left to right etc. Image a Windows Explorer in Large Icon mode where you see the folders and files as large icons in a grid.
I am using a third party component that is based on the ListView, so I have to use ListView methods to make this work.
How do I do it?
Update: Here is my code using the answer given:
<diag:NodeListView Name="nodeListViewSources" Width="400" Margin="0,0,0,0" Background="Gray" SelectionMode="Single" SelectionChanged="nodeListView_SelectionChanged">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</diag:NodeListView>
The NodeListView is a third party class derived from a ListView. Maybe that is the problem, but I thought it should work the same.
You just need to change the items' panel to a WrapPanel.
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"></WrapPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
I was able to get a hold of the third party component vendor. Turns out I also needed to add the following:
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
This along with the WrapPanel worked. Thanks to all...

Stackpanel add item animation

I've been struggling a while with marquee-style image scrolling control.
At a moment, I stuck up with templated ItemsControl:
<Window.Resources>
<DataTemplate x:Key="itemsTemplate">
<Image Source="{Binding AbsolutePath}"></Image>
</DataTemplate>
</Window.Resources>
<ItemsControl ItemTemplate="{StaticResource itemsTemplate}" x:Name="ic"
ItemsSource="{Binding ElementName=mainWindow, Path=DataItems}" VirtualizingStackPanel.IsVirtualizing="True">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical" VerticalAlignment="Bottom"
VirtualizingStackPanel.IsVirtualizing="True" >
</VirtualizingStackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
ItemsControl is bound to ObservableCollection, so I can add items at runtime. As soon as item goes off-screen it's removed from ObservableCollection.
The last thing to do is implementing custom item add behavior (smooth slide-in instead of insert-translateothers behavior).
Shall I derive from StackPanel to achieve such effect or just perform DoubleAnimation on currently adding item?
Any suggestions appreciated.
Check this out: Animate WPF Datatemplate when item added to Listbox. Will it suit your needs?

Resources