Display WPF (XAML) hierarchical data without TreeView - c#

Is there an equivalent to ItemsControl for hierarchical data (arbitrary depth)?
I want to avoid TreeView or such controls that offer expand/collapse/select functionality. I also don't want to fight against the default control implementations and re-template them writing pages of XAML.
PS: What I want can be done in code but I'd rather use the nice templates in XAML and type less.

Related

What's the difference between a stackpanel with datatemplates or a listview with listitems

I'm creating a WPF program that consumes rest api data. I want to implement lazyloading and infinite scroll on the data and programmatically create and add either custom data templates or listitems very similar to this design
I'm just confused as to which approach to take and what benefits/costs each provides
Easy choices:
Everyone uses MVVM so use MVVM.
Data Templating is a fundamental of wpf and building UI in code is not recommended - so use data templating.
You can dynamically add templates to resources by building xaml as strings. This is the MS recommended way to build any dynamic UI. Those strings can come from flat files, a database directly or a web service and you can build them by manipulating txt files or serialising controls.
A huge plus of this is you have the markup "right there". So when things go pear shaped you can paste into an experimental solution and see the errors light up in the xaml or see what the user is seeing.
If datatype associated templating doesn't suit for some reason then you could write a datatemplateselector and put your logic in there.
I'm not sure how you expect that to scroll exactly but I'd go with a listbox, some datatemplates associated with a type per view. Assuming the items can have different views - you just seem to have that "gilded" button or tag as an option.
Load your data into viewmodels with one per row.
.Add to an Observablecollection which is a public property in a viewmodel.
Bind that to the itemssource of a listbox.
They are then templated into UI.
A listbox has a scroller built in but you could re-template if you wanted to scroll using some other approach.
A StackPanel is a Panel that arranges child elements into a single line that can be oriented either horizontally or vertically.
A ListView is an ItemsControl that you can bind to an IEnumerable of objects and is used to present a collection of items.
What you should do is to create an ItemsControl with an ItemTemplate that corresponds to a scrollable item in the list. There is a basic example available here and you will find a lot more examples online.

How to organize MVVM in WPF application when a grid changes size at runtime, but the content of the grid are all of the same type

I have a WPF application that I've implemented using the MVVM pattern with a single windows which has a grid that can change dimensions at runtime. The content of the grid cells are always of the same type, however the number of columns in the grid vary depending on the state of the window. I'm new the MVVM pattern and WPF applications in general, so I'm having a hard time understanding how to represent the displayed data in a View Model, and to change the layout of the View accordingly.
The closest answer I've found is in this post which describes using DataTemplates to dynamically change the View based on the ViewModel bound to it, however it doesn't make sense to implement two separate ViewModels with the only difference being that one maintains a single data object, and the other maintains an array or list of the same data object.
WPF MVVM Why use ContentControl + DataTemplate Views rather than straight XAML Window Views?
I also know that I can programmatically configure the grid as in this post, but doing so in conjunction with a View model seems overly complicated, and like it breaks the MVVM pattern.
Programmatically setting the width of a grid column with * in WPF
Is there an approach to handling this problem that doesn't duplicate code or break the MVVM pattern?

class concept in WPF

I'm new in WPF and I need to group many components in one element and make and add new instance of that element in window for each student in database like 2 textblock plus 1 textbox for each student, how can i do something like that?
This is where WPF really shines - you can use an ItemTemplate or a DataTemplate to style the UI with the underlying data objects knowing absolutely nothing about how they are being presented.
Check out Data Templating for an introduction. Effectively an ItemTemplate is a template (definition) of how each item should be rendered. A DataTemplate goes a step further and gives you the ability to select which template to use based on the data item being bound to, so you can have a list containing different types of objects yet still show them all in the same list/repeater control on the screen.

dynamic binding of rows and columns to a grid in WPF

With a DataGrid object in WPF, we can bind its rows to an observable collection such that as rows are added or removed to/from the collection, the UI updates to display the changes. I am looking to do something similar with a Grid control as part of a User Control I am trying to create. Is this possible?
If you want to bind a collection to some sort of self updating UI in my experience an ItemsControl with custom ItemPanel and a ItemContainerStyle with a Template set as well as ItemTemplates for different kinds of objects in the collection is quite powerfulI.
It all depends on your exect scenario though. If you can elaborate i could whip up some XAML.

WPF: How do I perform custom rendering of items in List or Grid?

With Adobe Flex I would create a custom ItemRenderer to change the display of items in a grid. This executes some code that overrides how the grid control renders items. How do I do this with WPF? What are the performance implications with the techniques available?
My initial aim is to display an icon, a title, and a description. Ideally the description would be under the title. A similar UI is used by the Vista TaskDialog buttons.
Use Data Template. You can set it for ItemTemplate (ListBox), CellTemplate (GridViewColumn), HeaderTemplate, etc.
Performance depends on how complex the template is, but for what you described, you should not notice a difference.

Categories

Resources