Marginal Layout panel like Windows 7 Explorer ListView - wpf

In Windows Explorer in Windows 7, items in ListView has flexible margin. So all of icons fit in region of ListView.
How can i make a panel which implemented like this? WrapPanel is most approaching that, it's not flawless - a WrapPanel doesn't fit items to it's boundaries through adjusting margin.

Try using WrapPanel as your ListView's item panel and disable the horizontal scrollbar:
<ListView ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
...
</ListView>
ItemTemplate specifies how each item should be rendered. It has no effect on how items are laid out. ItemsPanel, by contrast, does specify the layout.
Also, you may want all items to be displayed the same size. You can find out how to do that from this article:
http://joshsmithonwpf.wordpress.com/2008/09/06/synchronizing-the-width-of-elements-in-an-itemscontrol/

Related

How do I bind a 1D array to a 2D display (datagrid or gird) in WPF?

I'm trying to set up databinding of a usercontrol. I have an item source List, its length is 64, I want to display it in 8x8 grids instead of 1x64 list because there is not enough room to display in UI. How to do that in WPF?
If they should all be the same size, then I would recommend using a UniformGrid. Here's an example of how you would use a UniformGrid in an ItemsControl:
<ItemsControl ItemsSource="...">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="8" Columns="8" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
You should be able to substitute ItemsControl for any of its descendants, such as ListBox.
If UniformGrid isn't exactly what you're looking for, my second suggestion would be WrapPanel. That should get you roughly the layout you want, but it wouldn't be a fixed 8x8 grid.
If neither of those panels work for you, I think the only remaining option would be to build your own.

WPF: Horizontally aligned collection with items of uniform width

I have a variable number of items bound from view model that need to be displayed horizontally and be selectable.
Each item is represented by a text, these texts vary in length. When I use a list view with a StackPanel with horizontal orientation as its ItemTemplate, the items are only as wide as the text inside.
Is there a way to make them all the same size, meaning the size of the widest one? Ideally without some complex codebehind, using item templates and such?
Note: I can't set some arbitrary minimum width, because I don't know what length the texts can ultimately have (different languages etc)
you can use ListBox which has selection support with UniformGrid as ItemsPanel. UniformGrid will allocate equal space for each element
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="1"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
example

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...

how do I stick the scrollbars of ScrollViewer to top in silverlight?

I have an Itemscontrol in my xaml inside a ScrollViewer.
<ScrollViewer Margin="0,0,0,0" BorderThickness="0">
<ItemsControl x:Name="itemsStackPanel">
<ItemsControl.ItemTemplate>
<DataTemplate>
<controls:UserItem Margin="0, 5, 0, 3"></controls:UserItem>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
and here is the list I am assigning to ItemsControl,
this.itemsStackPanel.ItemsSource = usersList;
now whenever new items are added in usersList, the UI updates and the scrollbar of ScrollViewer reaches at bottom. how do I stick it to top??
--EDIT--
one more issue I found, is that when ever the scrollviewer is resized horizontally, the scrollbars reach at bottom. how to keep the scrollbars on top while resizing?
One way would be to call
scrollView.ScrollToVerticalOffset(0);
MSDN page
However, this might just cause the list to scroll to the bottom and then the top again - not what you want.

How can I make a ListBoxItem stretch Vertically

I would like to make a ListBox function like a Grid. Each time a new item is added it should look like a a new GridRow was added (with a height of star). So if there are two items they will each take up half of the available space. At some point the Grid row will be smaller than the items MinHeight at which point the Grid will expand and a containing ScrollViewer can kick in.
You will see this behavior with a grid inside a ScrollViewer. However, I need to get this working with a ListBox so I can just set the ItemsSource, create a DataTemplate and move on.
The problem with the default ListBox ItemsPanel is that it will not let my first item expand to fill all the available space.
UPDATE:
Here's the code to get it working:
<ListBox VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Width="Auto" Height="Auto">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="1"></UniformGrid>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
This SO post has some pretty good information that seems relevant to your post
WPF - Why Listbox items do not fill uniformgrid

Resources