Some points to note
- Make properties
public , not private . - Make variables
private . - Follow naming conventions and don't add
class per class. ItemsSource you must be supplied in accordance with the data area. In my example, the user list is in the class scope, and I provided the ItemSource event when the window loads.
Here is the complete sample code. In this, I nested the Grid Control inside the ListBox, because later you can change the ListBox property for VirtualizingStackPanel. So this will give a huge performance boost if you have major updates on the list. You can also use a BindingList , which, in my opinion, is better than ObservableCollection performance.
User Class:
public class User : INotifyPropertyChanged { private int _key; private string _fullName; private string _nick; public int Key { get { return _key; } set { _key = value; NotifyPropertyChanged("Key"); } } public string NickName { get { return _nick; } set { _nick = value; NotifyPropertyChanged("NickName"); } } public string Name { get { return _fullName; } set { _fullName = value; NotifyPropertyChanged("Name"); } } public User(String nick, String name, int key) { this.NickName = nick; this.Name = name; this.Key = key; } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public override string ToString() { return string.Format("{0} {1}, {2}", Key, NickName, Name); } }
User List Class:
public class Users : ObservableCollection<User> { public Users() { Add(new User("Jamy", "James Smith", Count)); Add(new User("Mairy", "Mary Hayes", Count)); Add(new User("Dairy", "Dary Wills", Count)); } }
XAML:
<Grid> <Button Content="Start" Height="23" HorizontalAlignment="Left" Margin="416,12,0,0" x:Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> <ListBox x:Name="UserList" HorizontalContentAlignment="Stretch" Margin="12,41,12,12"> <ListBox.ItemTemplate> <DataTemplate> <Grid Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="20" /> <ColumnDefinition Width="150" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <TextBlock Text="{Binding Key}" Margin="3" Grid.Column="0" /> <TextBlock Text="{Binding NickName}" Margin="3" Grid.Column="1" /> <TextBlock Text="{Binding Name}" Margin="3" Grid.Column="2" /> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid>
XAML code behind:
public partial class MainWindow : Window { public static Users userslist = new Users(); DispatcherTimer timer = new DispatcherTimer(); public MainWindow() { InitializeComponent(); this.Loaded += new RoutedEventHandler(MainWindow_Loaded); } void MainWindow_Loaded(object sender, RoutedEventArgs e) { timer.Interval = DateTime.Now.AddSeconds(10) - DateTime.Now; timer.Tick += new EventHandler(timer_Tick); UserList.ItemsSource = userslist; } void timer_Tick(object sender, EventArgs e) { userslist.Add(new User("Jamy - " + userslist.Count, "James Smith", userslist.Count)); userslist.Add(new User("Mairy - " + userslist.Count, "Mary Hayes", userslist.Count)); userslist.Add(new User("Dairy - " + userslist.Count, "Dary Wills", userslist.Count)); } private void button1_Click(object sender, RoutedEventArgs e) { if (button1.Content.ToString() == "Start") { button1.Content = "Stop"; timer.Start(); } else { button1.Content = "Start"; timer.Stop(); } } }