Is it possible to see how a control created with XAML could be replicated with code behind? The reason I'm asking is that I would like to create a number of ListView controls based on each item in a Collection. My hope is that I can setup one ListView control in XAML and then somehow get the code that I would need to reproduce more Listview objects with the same settings in code-behind.
Alternatively; would it be possible to bind the Collection object containing all items that I want represented as ListView objects to any control that would then contain ListView controls for each item in the bound collection? Just the same way that a ListView can create ListViewItem controls if you bind a collection to the ListView control.
Cheers
Unless you have a very large hierarchy of controls, I recommend you to do it as follows: You create an List A which contains lists of your data, so A is List<List<Data>>
Then you create An Itemscontrol which is bound to this list. In the ItemTemplate, there is a ListView which its ItemsSource is bound to The DataContext.
Related
I'm developing windows metro app. In my application, I've one Listview with wrapgrid in itemspanel to display list of items on vertical rows with specific height. I want to display one item on top of the first column of list view, which shows result/stats of list items.
I would like to know if it is possible without adding custom item to datasource of listview?
ListView has a Header property in which you can Place content before the ListViews Items.
As commented, I've implemented following solution which is not elegant but worked for me.
Use Datasource converter to add dummy item in main list. So, my original list remains as is.
Use Template Selector to bind different template for first item.
Handle Selection and clicked event of dummy item.
I'm getting myself all in a pickle with TreeViews and User Controls; I'm fairly new to WPF so apologies in advance.
Synopsis
I have a collection of VM classes for my TreeView Items.
So the TreeView is bound to a collection of [Parent] VM instances, each of which has a collection of [Child]ren, and each [Child] has other data and other collections (which I won't bore you with).
The TreeView is on the left side of the form, and on the right I have a user control which should only be visible depending on the selected TreeViewItem type.
So, if the selected item of the TreeView is of '[Child]' type then the User Control should be visible.
Problem
I'm struggling with how to detect when an Item in the TreeView is selected so that I can show/hide the User control.
One way I thought of, but do not like, is to bind the 'IsSelected' property of the TreeViewItem to the [Child] VM class, then raise an event up to the main VM,
which would show/hide the UC via DP's. But this requires a whole load of Events etc and to me, just seems, messy.
Summary
For the life of me I cannot work out how to do this, I must be tired or something...or stupid...or both.
All I want to do is select an Item on the TreeView and the appropriate UserControl displays with the relevant data of the TreeViewItem, which I could in Windows Forms very easily, but obviously I'm not thinking about this in a very WPF'ish way at the moment.
Any links to articles etc appreciated.
Big up your chest for any responses.
In your case, i would prefer a DataTemplateSelector. It´s a object which provides a way to select a DataTemplate based on the databound object - here your ViewModel.
Just put a ContentControl in your Window and bind the Content-Property to the SelectedItem-Property of your TreeView. Set the ContentTemplateSelector-Property of the ContentControl to a reference of your own DataTemplate Selector.
The DataTemplateSelector will choose the correct DataTemplate with the defined UserControl by your requirements.
<DataTemplate x:key="VMParent">
<local:ParentUI DataContext="{Binding}" />
</DataTemplate/>
<DataTemplate x:key="VMChild">
<local:ChildUI DataContext="{Binding}" />
</DataTemplate/>
Greetings :)
In my wpf project I'm using a datatemplate (which consists of a textblock) as an itemtemplate to my listbox. The itemsource is a List of which there are 6 items. How can I loop through the 6 textblock's that are created at runtime?
Thanks
Do not do it.
Bind everything you need to change to your items and then just change the bound properties, messing with template controls is never a good idea, especially with virtualizing items controls where they may not even exist for all items.
(What you should not use: ItemsControl.ItemContainerGenerator.ContainerFromItem)
I have an ObservableCollection of items of class X. I have a user control to show to the user the information inside X. How can I create the binding so the user controls are created when data is added to the ObservableCollection?
Thanks.
Use and ItemsControl with an ItemTemplate containging the UserControl. Bind or set the ItemsSource to your collection.
See this reference for examples and in-depth explanaitions.
I am running into an issue where i am using a panorama control and binding it to a datasource. But i still do want other custom items on another panorama items where i need a textblock, a grid and so on. So if i am adding it in the backend it doesnt show up those panorama items. It just shows the datasource binded items. Why is that so? Both of them should work out.
Can anybody help me with a solution for this.
Thank You.
Since you're wanting to manually add PanoramaItems, I can think of two approaches:
Make sure your Panorama.ItemsSource is set to an ObservableCollection that is accessible in the code behind or viewmodel, and then add your new items to that ObservableCollection which should update the Panorama.
Don't databind the Panorama control's Items - just add items manually when you want them.
Either way, the Panorama's ItemTemplate gets evaluated when the items are added to the underlying collection, so using a DataTemplateSelector will allow your code to determine which DataTemplate to apply when the new item is added without affecting the templates for previous items.
/chris