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)
Related
Background: I have a WrapPanel with Vertical Orientation that I want to put up to 40 "items" in. Each "item" within the WrapPanel will contain a combination of buttons and expandable lists in a 5x6 grid format. I need to be able to select each "item" within the WrapPanel so it brings up a dialogue box where the user can click the buttons and edit the expandable lists for that "item".
Question: I understand that the main difference between ItemsControl and ListBox is that the entities inside a ListBox are selectable while the ones in ItemsControl are not, however I do not need to select values inside my ItemsControl or ListBox until my dialogue appears, so I'm not sure if it would be best to use the ItemsControl or ListBox in this situation. Which ListControl would be best for my situation?
If your root items should be selectable you would probably want a ListBox whose ItemsPanel is a WrapPanel. If you don't need actual selection (i.e. it does not matter that items automatically deselect, or that operations are executed on the set of selected items) you can just use an ItemsControl with a WrapPanel as ItemsPanel and use a big Button as the root of your ItemTemplate (to trigger the dialog).
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.
I'm not being able to update a single item's DataTemplate on a list during runtime. In detail, here is what I'm trying to accomplish.
I have a Listbox where the items can have different states (Collapsed, expanded, disabled, etc), each with a different layout. I'm using a TemplateSelector to choose the correct DataTemplate according to a property on my class and that's working all great when I first create the list, the items are shown properly. However, when I change the property that sets the DataTemplate in runtime, the NotifyPropertyChanged is called and the information of the item is updated on the list, but not the DataTemplate. For example: I have a collapsed item with a label X that i want to expand. I click on the item and the label changes to Y but the DataTemplate doesn't update.
Any idea on how I can do this? Can't the DataTemplate be updated during runtime unless it is for the whole list?
I'll appreciate any help.
Make UserControl and use it inside your data template. Now, to change the state you can call methods on this UserControl and it will update. You can use animations via storyboard too.
I have a DataGrid with a bunch of rows representing items in my system. I want that each time a user selects an item in the grid (the user is allowed to select multiple items) the item will be added to a StackPanel and have its own datatemplate using an Expander to display its data.
Can anyone help me do this in WPF?
Thanks!
John.
I wouldn't use a StackPanel, but an ItemsControl, and bind its ItemsSource to the DataGrid's SelectedItems. However, at the moment I'm not sure whether SelectedItems has change notification. If not, you might have to use a CollectionViewSource in between, and call CollectionViewSource.Refresh during the DataGrid.SelectionChanged event.
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