How to make ListView items arrange in grid format - c#

I want to display a list of items in a table like format. The format is fluid such that if a user resizes then more or less items will display on a row. I want to replicate how floated divs display within a container.
How do I do this in WPF?
So far I can get the items to display in a vertical format but cant figure out what to do next. P.s. for now I am hard coding the items whilst I debug, but I will bind it to a list at some point.
<ListView>
<ListViewItem Width="244">
<local:ScheduledRecordingView />
</ListViewItem>
<ListViewItem Width="244">
<local:ScheduledRecordingView />
</ListViewItem>
</ListView>

Surely been asked before, please search before asking.
Just make the ItemsPanel a WrapPanel. If you don't create a specific View (not necessary here) you should use a ListBox or if you don't need selection either an ItemsControl.

Related

WPF ListView virtualization in a ScrollViewer

I have a requirement which is to display a user information at top of the page and a ListView of images will follow it, and I've wrote following code (it's a pseudocode but I think it's enough to explain what I've done):
<ScrollViewer>
<StackPanel>
<Grid>
<!-- User Information Part -->
</Grid>
<ListView>
<!-- Images Part, This is a custom virtualized ListView, it's ItemsPanel is a custom VirtualizingWrapPanel -->
</ListView>
</StackPanel>
</ScrollViewer>
But in this scenario, the VirtualizingWrapPanel (by which has been tested on another individual ListView without an explicit ScrollViewer declaration and it works correctly) and the virtualization of ListView won't work because the desired height of ScrollViewer is positive infinity and all the items in the ListView will be expanded and rendered, I wonder whether there is a way that can make the ListView in ScrollViewer being virtualizable? Thanks
You can't virtualize a list that has all elements being rendered (because of the StackPanel),
A workaround that will work for you: you need a single ListView. With the first row customized to display the User Information Part, and all other rows displaying images.

Changing ItemsPresenter in custom ComboBox style to display items next to each other

I want ComboBox to contain Images as items (they will be added programatically). In default ComboBox they are displayed vertically. I would like to have them to be displayed like in VariableSizedWrapGrid but I'm not exactly sure how to do it.
I have this ItemsPresenter for ComboBox style but changing it to anything else results in nothing being displayed.
Or maybe I should make it other way (using ListView?). I want selected item to be displayed like in ComboBox and after click all items displayed at once (around 25).
How it looks now:
What I want:
Change the ItemsPanel property:
<ComboBox>
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
If you want to use something like VariableSizedWrapGrid you can create your own implementation or check out other peoples work for example: VariableSizedWrapGrid for WPF.
I've made some research and it seems it's not possible to achieve what I want. I'll just do it another way to get what I want (Button with Flyout and ListView inside it should do the work).

how to add user controls as listbox items

I am using WPF .net 4.5 (c#) and I would like to make a ListBox that contains a series of user controls. (If a listbox is the wrong type of control, please let me know).
I want my listbox to have a copy of the user control as the list items, with different contents within each one.
How do I add user controls to a listbox?
Thanks for your help in advance!
You can set ItemTemplate for the listbox with your usercontrol in it.
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<local:UserControl1/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Thanks
I think you can design a DataTemplate which ha the same UI or Style as this UserControl .
maybe just copy paste can get a DataTemplate as you want,however
<DataTemplate>
<local:UserControl1/>
</DataTemplate>
this king seems very strange for me I dont konw it can work as you want,so I also want to know
the answers。

How to access child textbox inside cell on RadGridView

I'm using a telerik RadGridView which is pretty much the same thing as a normal DataGrid in WPF. In my gridview.columns I have a GridViewDataColumn which then allows me to put a celltemplate then a datatemplate and then allow me to put different controls within a grid. I have a combobox and a textbox(only one shows at a time based on visibility property). The problem I'm having is the tab system is kind of weird and doesn't work right. When I tab to a cell in the column above, my combobox nor my textbox ever gets focus. In fact the cell turns completly white. So I was wondering how (in code behind) can I detect when a user tabs in this particular cell and manually set focus to these child elements inside this cell on the selected row?
<telerik:GridViewDataColumn x:Name="MyDataColumn" Focusable="True" GotFocus="MyDataColumn_GotFocus_1" Header="Header1" Width="250">
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<Textbox x:name="MyTextbox" Visibility="{Binding IsTextbox}"/>
<Combobox x:name="MyCombobox" Visibility="{Binding IsCombo}"/>
</Grid>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewDataColumn>
*Basically, how can I gain access to one of those child controls inside this GridViewDataColumn in code behind so that I can set focus to it? Thanks so much for any advice.
Probably the most straight forward answer to your question can be found by reading the answer to the Access items inside the DataTemplate in WPF post.
However, it may be worth reading the correct answer in this Access Elements inside a DataTemplate… How to for more than 1 DataTemplate? post also.

how to add two different controls (checkbox and combobox) as list item in ListBox wpf

I need to bind the listbox with list of items retreived from database. Each list item is displayed as checkbox in listbox.
<ListBox.ItemTemplate>
<HierarchicalDataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}"/>
</HierarchicalDataTemplate>
</ListBox.ItemTemplate>
Along with the checkbox, I also need to add a checkbox as a list item.
Could anyone let me know as to how this can be done?
DataTemplateSelector options within WPF this allow a very small amount of backend code to take care of all the selections for you. This allows you to make use of this code from within an MVVM structure without any issues, and while making the front end extremely easy to manage without complicating the back end code.
here is a sample.
Use DataTemplateSelector if you want to use two different visuals as list items.

Categories