Answered by:
creating listview dynamically

Question
-
I am new to wpf and I want a to create a listview where the items of the listview is dynamically generated. Each of the items should contain Textblocks images buttons and so on.Can anyone suggest how to go about it? Thanks in advance.Monday, October 13, 2014 6:34 AM
Answers
-
Create a list view and bind ItemSource to an ObservableCollection, now anything you add to the observable collection during runtime will reflect in the list view.
You can change ListView.ItemTemplate to display anything of your interest. refer Styling and Templating<Window x:Class="TempMSDN.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <ListView ItemsSource="{Binding Countries}"> <ListView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding}"/> <Button Content="Click Here"/> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView> </Grid> </Window>
using System.Collections.ObjectModel; using System.Windows; namespace TempMSDN { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Countries = new ObservableCollection<string>(); Countries.Add("US"); Countries.Add("GB"); DataContext = this; } public ObservableCollection<string> Countries { get; set; } } }
Hope it helps!
Regards Vallarasu S. BreakingDotNet.blogspot.com
- Proposed as answer by Pete LakerMVP Monday, October 13, 2014 10:31 AM
- Marked as answer by Franklin ChenMicrosoft employee Thursday, October 23, 2014 8:34 AM
Monday, October 13, 2014 7:15 AM
All replies
-
Create a list view and bind ItemSource to an ObservableCollection, now anything you add to the observable collection during runtime will reflect in the list view.
You can change ListView.ItemTemplate to display anything of your interest. refer Styling and Templating<Window x:Class="TempMSDN.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <ListView ItemsSource="{Binding Countries}"> <ListView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding}"/> <Button Content="Click Here"/> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView> </Grid> </Window>
using System.Collections.ObjectModel; using System.Windows; namespace TempMSDN { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Countries = new ObservableCollection<string>(); Countries.Add("US"); Countries.Add("GB"); DataContext = this; } public ObservableCollection<string> Countries { get; set; } } }
Hope it helps!
Regards Vallarasu S. BreakingDotNet.blogspot.com
- Proposed as answer by Pete LakerMVP Monday, October 13, 2014 10:31 AM
- Marked as answer by Franklin ChenMicrosoft employee Thursday, October 23, 2014 8:34 AM
Monday, October 13, 2014 7:15 AM -
>>I am new to wpf and I want a to create a listview where the items of the listview is dynamically generated
The visual items in a ListView is created based on the objects in its ItemsSource. The ItemTemplate of the ListView defines the apperance for an object in the ItemsSource collection.
This means that if you want each of the items to contain TextBlocks, Images and Buttons, you should put these elements in the ItemTemplate of the ListView, e.g.:
<ListView x:Name="lv"> <ListView.ItemTemplate> <DataTemplate> <StackPanel> <Image /> <Button Content="..."/> <TextBlock Text="{Binding}"/> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView>
Then it is just a matter of setting its ItemsSource to some collection that contains objects, e.g:
public MainWindow() { InitializeComponent(); lv.ItemsSource = new List<string> { "a", "b", "c" }; }
Please remember to mark helpful posts as answer and/or helpful.Monday, October 13, 2014 10:31 AM