Click here to Skip to main content
15,886,761 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: ListView issues Pin
#realJSOP6-Mar-10 9:35
mve#realJSOP6-Mar-10 9:35 
GeneralRe: ListView issues Pin
Eslam Afifi6-Mar-10 10:51
Eslam Afifi6-Mar-10 10:51 
GeneralRe: ListView issues Pin
Pete O'Hanlon6-Mar-10 11:14
mvePete O'Hanlon6-Mar-10 11:14 
GeneralRe: ListView issues Pin
Eslam Afifi6-Mar-10 12:27
Eslam Afifi6-Mar-10 12:27 
GeneralRe: ListView issues Pin
Pete O'Hanlon6-Mar-10 12:34
mvePete O'Hanlon6-Mar-10 12:34 
GeneralRe: ListView issues Pin
Eslam Afifi6-Mar-10 13:02
Eslam Afifi6-Mar-10 13:02 
GeneralRe: ListView issues Pin
Pete O'Hanlon6-Mar-10 9:41
mvePete O'Hanlon6-Mar-10 9:41 
AnswerRe: ListView issues Pin
Pete O'Hanlon6-Mar-10 10:22
mvePete O'Hanlon6-Mar-10 10:22 
John - you only need an ObservableCollection if you want to catch notification changes. In this case, you'd bind to the properties in exactly the same way you'd bind to an ObservableCollection. Here's a quick sample I whipped together for you:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BindToList
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
  {
    private List<MyListItem> _list;
    public Window1()
    {
      InitializeComponent();

      _list = new List<MyListItem>();
      AddItem("John", "Simmons");
      AddItem("Pete", "O'Hanlon");
      AddItem("Clint", "Eastwood");

      foreach (MyListItem item in _list)
      {
        listBound.Items.Add((MyListItem)item);
      }

    }

    public List<MyListItem> MyList
    {
      get { return _list; }
      set { _list = value; }
    }

    public void AddItem(string forename, string surname)
    {
      _list.Add(new MyListItem { ID = _list.Count + 1, Forename = forename, Surname = surname });
    }
  }

  public class MyListItem
  {
    private int _id;

    public int ID
    {
      get { return _id; }
      set { _id = value; }
    }
    private string _forename;

    public string Forename
    {
      get { return _forename; }
      set { _forename = value; }
    }
    private string _surname;

    public string Surname
    {
      get { return _surname; }
      set { _surname = value; }
    }
  }
}
Now, here's the XAML that goes with this:
<Window x:Class="BindToList.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Window1" Height="300" Width="300">
  <Window.Resources>
    <DataTemplate x:Key="myListTemplate">
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding ID}" />
        <TextBlock Text="{Binding Forename}" />
        <TextBlock Text="{Binding Surname}" />
      </StackPanel>
    </DataTemplate>
  </Window.Resources>
  <Grid>
    <ListBox x:Name="listBound" 
       ItemTemplate="{StaticResource myListTemplate}" 
       Width="200" />
  </Grid>
</Window>

"WPF has many lovers. It's a veritable porn star!" - Josh Smith

As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.


My blog | My articles | MoXAML PowerToys | Onyx



QuestionDisplay System.Windows.Forms.Form as child of Wpf Window Pin
abhinish6-Mar-10 1:01
abhinish6-Mar-10 1:01 
AnswerRe: Display System.Windows.Forms.Form as child of Wpf Window Pin
#realJSOP6-Mar-10 2:17
mve#realJSOP6-Mar-10 2:17 
GeneralRe: Display System.Windows.Forms.Form as child of Wpf Window Pin
abhinish6-Mar-10 5:00
abhinish6-Mar-10 5:00 
GeneralRe: Display System.Windows.Forms.Form as child of Wpf Window Pin
#realJSOP6-Mar-10 5:29
mve#realJSOP6-Mar-10 5:29 
GeneralRe: Display System.Windows.Forms.Form as child of Wpf Window Pin
Valentin Billotte31-May-10 4:51
Valentin Billotte31-May-10 4:51 
QuestionBackgroundWorker and comboBox.Items.Add(f) Pin
HahnTech5-Mar-10 15:35
HahnTech5-Mar-10 15:35 
AnswerRe: BackgroundWorker and comboBox.Items.Add(f) Pin
#realJSOP6-Mar-10 2:18
mve#realJSOP6-Mar-10 2:18 
GeneralRe: BackgroundWorker and comboBox.Items.Add(f) Pin
HahnTech8-Mar-10 12:31
HahnTech8-Mar-10 12:31 
QuestionMessage Removed Pin
5-Mar-10 3:38
professionalN_tro_P5-Mar-10 3:38 
AnswerRe: ResourceDictionaries Pin
Pete O'Hanlon5-Mar-10 4:34
mvePete O'Hanlon5-Mar-10 4:34 
GeneralMessage Removed Pin
5-Mar-10 8:33
professionalN_tro_P5-Mar-10 8:33 
GeneralRe: ResourceDictionaries Pin
Pete O'Hanlon5-Mar-10 9:41
mvePete O'Hanlon5-Mar-10 9:41 
QuestionDatabinding to a property which is a collection. Pin
ddecoy5-Mar-10 2:37
ddecoy5-Mar-10 2:37 
AnswerRe: Databinding to a property which is a collection. Pin
Pete O'Hanlon5-Mar-10 2:58
mvePete O'Hanlon5-Mar-10 2:58 
GeneralRe: Databinding to a property which is a collection. Pin
ddecoy5-Mar-10 3:08
ddecoy5-Mar-10 3:08 
QuestionCreating Custom Control in WPF (.Net 3.5) Pin
Mehdi Ghiasi4-Mar-10 23:13
Mehdi Ghiasi4-Mar-10 23:13 
AnswerRe: Creating Custom Control in WPF (.Net 3.5) Pin
Pete O'Hanlon4-Mar-10 23:21
mvePete O'Hanlon4-Mar-10 23:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.