WPF Listbox change container at runtime - c#

For my project i need a scatterview but let's call it a listbox for people who don't work with the surface sdk :) , and it's bound to an observablecollection of objects in my viewmodel. Depending the kind of object, i use a different datatemplate (images, video's,...)
The container for these items is by default a listboxitem.
But there is a special object where i need more than a normal listboxitem as container, so i created my customlistboxcontainer. Here if found how to use that container:
Have a databound WPF Listbox generate subclassed ListboxItems
but this is not enough. I need to check at runtime what item is added to the collection, and choose the correct container for it (listboxitem, customlistboxitem). I can't seem to get is work.
Has anybody some experienxe with this?
Thx,
Bjorn

If you need to provide a different template for different types of items I suggest providing a DataTemplateSelector for your ListBox. I think the MSDN example is pretty clear on how to implement it, but let us know if you need further help.
Update:
Here's an another tutorial to get you started.

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.

Reusing visual tree when moving items between two ItemControls

I have two ObservableCollections in my ViewModel. Both of same generic type.
Window has two ItemsControls each with ItemsSource bound on its own ObservableCollection from ViewModel.
Both use same same ItemTemplate. This ItemTemplate is very complex user control.
It's all done by the book.
In some cases, I move item from one collection to another. UI updates correctly. However, as control is complex, it needs about 1-2sec to render when it is recreated in new ItemsControl. And since this happens on drag and drop operation, it is not user friendly.
What I would like to do is to somehow preserve existing instance of ItemTemplate from source ItemsControl and reuse it in destination. I don't know if it is even possible, I couldn't find anything about it online.
I believe setup is fairly simple, but if needed I'll write some simplified version of code (I can't share the real code).
Thanks for any help.
What I would like to do is to somehow preserve existing instance of ItemTemplate from source ItemsControl and reuse it in destination. I don't know if it is even possible, I couldn't find anything about it online.
I am afraid it is not. When you disconnect an element from the visual tree and then add it back to a different parent element at a later stage, all elements that make up the control will be re-rendered.

How to dynamically update a listviewitem through code

I have a listview and would like to update the text of one of the columns for a specific listviewitem (row).
How would I go about doing this?
Hard to say without any context because there are so many ways you could populate your list!
The generic answer is you bind your list to a collection view which itself binds its source to your viewmodel (or you bind directly to your viewmodel if you don't need CollectionView features).
When you want to modify your list, you make sure you raise a modification notification on your property, and XAML binding will take care of updating everything.
It is really basic stuff on dependency property and binding, you should read more about this topic. MVVM-light is a very light framework that allows you to take care of all kinds of binding-related issues with a very clean and neat flavor. You will also find some very good self-explanatory webcasts from the author of the website about all those topics.

Silverlight ChildWindow with DataTemplate

I am creating a custom ChildWindow that I want to use with a DataTemplate.
The DataTemplate will apply to the "body" of the window, but then, separate from that, I want to always display two buttons, "Save" and "Cancel".
I have no idea how to accomplish that... Any help would be greatly appreciated!
Grab a copy of your ChildWindowStyle from your SdkStyles.xaml to give you a foundation for building your custom control template on. To keep a DataContext you could throw it in a UserControl as UserControl.Resources or if you're just populating ContentPresenters etc you can put the template in your own resource dictionary or wherever you like (though you might want to specify a unique x:Key name for it.) Just depends on how you'd like to use it.
Make your desired changes to the template and also add your Buttons etc. Then you can either set it as the default by replacing the Default BasedOn value in your resource dictionary to point to it or call that style explicitly.
Personally I prefer Expression Blend for all of this and there's even some tutorials out there to help you along with a Web Search (which I might suggest first next time.) Like what you might find here... Hope this helps! :)

Setting the datasource of a WPF Grid

I'm very new to WPF, and am trying to set the datasource (which the WPF Grid doesn't have as a property) of my grid to take a List. Does anyone have any code examples of how to do this. I have googled it, but can't find any really good examples.
(Oh, and can anyone suggest a good site for all round WPF Code examples?)
Thanks
If you're referring to a WPF Grid, you can't bind it to data; it's meant for layout purposes only; you might want to look into one of the controls that inherit from ItemsControl, such as ListView.
The property you'd bind your list to, is called ItemsSource.
The other control you might be thinking of is the GridView
There's also the DataGrid (Note old link) in the WPF Toolkit which implements a lot of the same functionality as the WinForms DataGridView

Categories

Resources