Bind List of UControls to a Controls Children property? - wpf

I have got an ObservableCollection<Button> buttonList; (this can be any other control: TextBox, TextBlock or even a UserControl).
I want to bind this List via DataBinding to a parent control (lets say a Grid or a WrapPanel,...)
Is this somehow possible? Children property is read only.
I do not want to do this in the program code -> Dont iterate through the list and add every item to the children property.
I want to BIND the List to a parent control -> the View is automatically updated, every time I add a new Item to the List.
Any Ideas?

Use ItemsControl, and bind your collection to ItemsSource.
And don't forget to read Dr. WPF's articles: ItemsControl: A to Z. This is second time I'm mentioning this blog today here, but he really-really deserves to be mentioned in every post related to collection of items and bindings :).
Cheers, Anvaka.

Related

PropertyGrid-like multiselection in other elements(listView)

i want to immitate the behavior of multiselection of items (eg. in a list view) and their appearence in a PropertyGrid. So, obly the same properties are shown and values set in the grid are set on all multiselected properties.
I have created a listview with some datatemplates and have in another listview some data(with databinding). Now i want wo have a multiselection on the listview with the data and have the properties shown in the another listview like in a propertygrid.(explanation: The ProprtyGrid is still not available in WPF and if has not the flexibility of showing data the way i need it)
So, how do i need to prepare my data to be shown in the list propertygrid-multiselection-style? Is that even possible???
Greets,
Jürgen
I'm not sure if this question is still active, or what not. But I have found a solution that works for me - and I need somewhere to share it.
The first issue you are going to have solve is selecting the items, ie. multi-select. See my tip here that explains how to do that.
Next, I guess its basically binding to listview's selectedItem property and how you want to update your propertyGrid based on changes to the selectedItem prop.
Hope this helps.

Change the selected item of a treeview in WPF

I have a treeview with several items in it. How do I force the treeView to select a specific item ? Everytime I try to use any of the "Selected..." property, I get the error that it is read-only and cannot be set either in code or in XAML.
Regards,
You need to create a Selected property on your tree item's ViewModel, and bind to it.
Here is an article that explains how to do it.
Edit. Actually that uses a different way. The article I was thinking of was this one

WPF TabControl rebinding every time

I am data templating a tab control. Each time I select a tab, the binding of the contents get applied. So for eg, if I have a tree view expanded in tab1 and going to tab2 and coming back has this collapsed. The property binded to ItemsSource is invoked each time I flip the tabs.
learned its all because the visual tree gets recreated again and again as selectd item changes.
Now the qn is, any easy way to fix this. Wil be really helpful.
One way to go is to have an 'IsExpanded' property for a TreeViewItemViewModel and bind to it. Basically your viewmodel will hold the expanded/collapsed state information in your viewmodel.

HeaderedContentControl ItemsSource

Why there is no HeaderedContentControl ItemsSource property?
How then can I databind to a list of objects to be represented by HeaderedContentControl?
Thanks
John
Because ItemsSource is all about multiple pieces of content, and HeaderedContentControl has one piece of content. Put another way, HeaderedContentControl's job is to present a single object, so it doesn't need a property whose job is to feed it multiple pieces of content.
Use HeaderedItemsControl instead, or (depending on your requirements) an ItemsControl where the DataTemplate is a HeaderedContentControl. (Though in the latter case you might as well just use a Panel and multiple elements within the panel -- the HCC isn't really buying you anything.) HIC's job is to present multiple items under a single header, and it's pretty flexible. For example, the framework uses HIC as the base class for both TreeViewItem (whose "header" is the item at hand, and whose "items" are the children of that item) and MenuItem (whose "header" is the menu item, and whose "items" are any sub-menu-items, for example in a drop-down or pop-out menu).
There's no ItemsSource property, since it can have only a single child (or two children if you count Header in), just like the class it inherits from - ContentControl. Use Content property instead.
You can find more about it on MSDN.
If you want to display list of objects within HeaderedContentControl, then just add ListBox as its Content and fill ListBox with objects.
Maybe you need a HeaderedItemsControl.
You can find a sample here.
You should be able to bind the collection to the Content property.
HeaderedContentControl by name itself is a collection of ContentControl each with Header.

Append Items to Databound ItemsControl in WPF

I've got a Combo Box that is databound to an ObservableCollection of items. I would like to have a default selected item that is (None) that would set the value of the property I've bound to "SelectedValue" to null.
I think there ought to be a way to achieve this with some combination of Style/DataTemplate/TemplateSelector. I'm trying to design this with MVVM in mind, so I'd like something that doesn't use codebehind and is as reusable as possible. I'd also like the benefits of the ObservableCollection (updating the collection causing the control to rebind) to remain intact.
Bonus part B:
I would like to also be able to append an extra visual element to the bottom of an ItemsControl as well. I was thinking it would be easy to change the DataTemplate if I knew how to trigger it on the last item of a collection. Willing to entertain other options here.
The simplest way I've found to do this is to insert a "special" value into the underlying collection, and display the "(None)" text when it's selected. Obviously then you need to run your binding through a converter to take this value into account and return null when it's selected. (See this question of mine which was a result of me trying to add an actual null value to a ComboBox's underlying collection.)
Having said that, it might actually be possible to do what you want with the CompositeCollection class. You could make a separate collection (with only one item - your Null item) and bind your ComboBox to both it and your original collection through the CompositeCollection.

Resources