Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. (ANSWERED) (newbie) I'm confused about ListView ItemSource bindings.

(ANSWERED) (newbie) I'm confused about ListView ItemSource bindings.

Scheduled Pinned Locked Moved WPF
wpfdevops
3 Posts 2 Posters 10 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #1

    (Again, another complete newbie). I'm trying to bind an ItemSource to a ListView. From what I understand, the data should be accessible via a Property. The main Model is "MyModel" it contains a list of items. (eventually will contain lot more data). I'm not sure what incantation I'm missing. Thanks.

    public class Item
    {
    public string Name { get; set; }

    public Item(string name)
    {
    	Name=name;
    }
    

    }

    public class ItemsList
    {
    public List Items
    {
    get; private set;
    }

    public ItemsList()
    {
    Items = new List
    {
    new Item("a"),
    new Item("b"),
    new Item("c")
    };
    }
    }

    public class MyModel
    {
    private readonly ItemsList _list;

    public List Items => \_list.Items;
    
    public MyModel()
    {
    	\_list = new ItemsList();
    }
    

    }

    App.xaml.ca :

    protected override void OnStartup(StartupEventArgs e)
    {
    MainWindow = new MainWindow()
    {
    DataContext = new MyModel()
    };

    MainWindow.Show();
    base.OnStartup(e);
    

    }

    MainWindow :

    MyListView.xaml component. :

    CI/CD = Continuous Impediment/Continuous Despair

    Richard DeemingR 1 Reply Last reply
    0
    • M Maximilien

      (Again, another complete newbie). I'm trying to bind an ItemSource to a ListView. From what I understand, the data should be accessible via a Property. The main Model is "MyModel" it contains a list of items. (eventually will contain lot more data). I'm not sure what incantation I'm missing. Thanks.

      public class Item
      {
      public string Name { get; set; }

      public Item(string name)
      {
      	Name=name;
      }
      

      }

      public class ItemsList
      {
      public List Items
      {
      get; private set;
      }

      public ItemsList()
      {
      Items = new List
      {
      new Item("a"),
      new Item("b"),
      new Item("c")
      };
      }
      }

      public class MyModel
      {
      private readonly ItemsList _list;

      public List Items => \_list.Items;
      
      public MyModel()
      {
      	\_list = new ItemsList();
      }
      

      }

      App.xaml.ca :

      protected override void OnStartup(StartupEventArgs e)
      {
      MainWindow = new MainWindow()
      {
      DataContext = new MyModel()
      };

      MainWindow.Show();
      base.OnStartup(e);
      

      }

      MainWindow :

      MyListView.xaml component. :

      CI/CD = Continuous Impediment/Continuous Despair

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Maximilien wrote:

      <components:MyListView DataContext="MyModel"/>

      That line's wrong for a start. The DataContext has already been set for the window, and will be inherited by the MyListView control.

      Maximilien wrote:

      <ListView Margin="10" ItemsSource="{Binding Items}"/>

      That looks correct. Are you getting any binding errors in the output window? XAML data binding diagnostics - Visual Studio (Windows) | Microsoft Learn[^] NB: Your view-models should really implement INotifyPropertyChanged[^], and the collection should be an ObservableCollection<T>[^], so that the binding system can pick up any changes. But at the moment you don't seem to be making any changes, so that's unlikely to be the issue.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Maximilien wrote:

        <components:MyListView DataContext="MyModel"/>

        That line's wrong for a start. The DataContext has already been set for the window, and will be inherited by the MyListView control.

        Maximilien wrote:

        <ListView Margin="10" ItemsSource="{Binding Items}"/>

        That looks correct. Are you getting any binding errors in the output window? XAML data binding diagnostics - Visual Studio (Windows) | Microsoft Learn[^] NB: Your view-models should really implement INotifyPropertyChanged[^], and the collection should be an ObservableCollection<T>[^], so that the binding system can pick up any changes. But at the moment you don't seem to be making any changes, so that's unlikely to be the issue.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        Yeah, I'm using INotifyPropertyChanged and ObservableCollection (did not want to clutter the example) Ohhhh !!! thanks. :rose::rose: Fixed. I read that (passing the modelview to components) on a tutorial: See: [youtube-viewers/MainWindow.xaml at master · SingletonSean/youtube-viewers · GitHub](https://github.com/SingletonSean/youtube-viewers/blob/master/YouTubeViewers.WPF/MainWindow.xaml) And at the end of : [youtube-viewers/YouTubeViewersView.xaml at master · SingletonSean/youtube-viewers · GitHub](https://github.com/SingletonSean/youtube-viewers/blob/master/YouTubeViewers.WPF/Views/YouTubeViewersView.xaml)

        CI/CD = Continuous Impediment/Continuous Despair

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups