(ANSWERED) (newbie) I'm confused about ListView ItemSource bindings.
-
(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(); }
}
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
-
(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(); }
}
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
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 theMyListView
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
-
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 theMyListView
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
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