ListView with Rows and Columns - wpf

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

Related

Binding several ItemsControl to one ObservableCollection

I have two tabs.
The code of both tabs is in the same XAML file (yes, it must be)
Each tab references the same ObservableCollection
<ItemsControl ItemsSource="{Binding ConnectorItems}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Depending on the tab, the collection is reloaded and contains other data
The problem is that it only displays in the last tab.
I know that this results from logical parents but I am looking for a solution in this situation
If I had a bookmark code in a separate XAML it would not be a problem but I can not use it

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>

Dynamically show a different amount (0-10) BitmapImages on a View

I have a small problem designing problem:
My card tool should display 0-10 images on my view.
Sometime just three, sometimes six, sometimes all ten...
Due to MVVM I don't want to just Controls.Add() the images to the grids/controls content.
What is a good approach to this? One way would be having ten bitmap variables, but is there another better way?
Edit:\
I tried it like that:
<ItemsControl ItemsSource="{Binding CardImages}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"></WrapPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}" Stretch="Uniform" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
But its not working as it should.
I have SIX pictures, but the rest of the pictures arent visibilty (out of sigth). I want to show all of the pictures, so the 3 have to be about 50 % smaller in height and the other three atm not visible pictures should be shown under the shown three pictures. Hope thats clear.
How to solve that? I tried ScrollViewers, ViewBoxes and some different kind of templates / panels, but had no sucess. :(
I would bind an ItemsControl to a public observable collection property that contains your image sources / locations / however you reference them on your view model and then template the items to display images.
<ItemsControl Binding="{Binding MyPublicProperty}">
<ItemsControl.ItemTemplate>
<Image Source="{Binding Source={StaticResource MyImage}, Path=Source}"/>
</ItemsControl.ItemTemplate>
</ItemsControl>
See this SO question for binding the image.

WPF Listbox Wrapping

I have a listbox in which I use a ListBox.ItemsPanel - WrapPanel.
<ListBox ItemsSource="{Binding Path=Applets}" Margin="10,92,10,10" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical" IsItemsHost="True">
</WrapPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>...
I am trying to have the wrappanel have a behavior such that the items fill in to the right as the width is made wider and wrap as needed when the window is made narrower. I have played with it but the correct combination eludes me. Does anyone have a suggestion?
My next goal would be able to reorder/ sort the items and have the render update.
TIA
I am trying to have the wrappanel have a behavior such that the items fill in to the right as the width is made wider and wrap as needed when the window is made narrower. I have played with it but the correct combination eludes me. Does anyone have a suggestion?
The code you have is almost correct, just change the Orientation to Horizontal and it should work as you describe
My next goal would be able to reorder/ sort the items and have the render update.
You don't have to do anything special for that, it's the normal behavior of a ListBox. Just change the sort order (using ICollectionView.SortDescriptions), and the UI will reflect the changes
<ListBox Grid.Row="1" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBoxItem Name="lbiTmp3_1"><CheckBox>
<TextBlock TextWrapping="Wrap">
lkjfd gmlkdsfmlk gmdsgf kds lkjglfdjmlkg jfdsg dsgf lkhfdgs lkjds fg
</TextBlock></CheckBox>
</ListBoxItem>
<ListBoxItem Name="lbiTmp3_2">C0ucou</ListBoxItem>
<ListBoxItem Name="lbiTmp3_3">C0ucou</ListBoxItem>
</ListBox>

WPF WrapPanel/StackPanel with DataTemplate?

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

Resources