stalbaig stalbaig - 3 days ago 4
C# Question

Data binding not working and cannot see any error

I am following a tutorial on WPF from msdn (https://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx). I am not getting any error but no output either. I wish to see the data in the ListBox from the properties that are assigned in the code. I am pasting both of my xaml and cs file below. Any help in this regards is appreciated. I would appreciate if someone could refer me to some tutorials on WPF other than msdn.

MainWindow.xaml

<Window x:Class="Practice_1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:Practice_1"
mc:Ignorable="d"
Title="MainWindow" Height="150" Width="300">
<Window.Resources>

</Window.Resources>

<ListBox Width="Auto" Height="Auto">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Description}"/>
<TextBlock Text="{Binding Path=StartPrice}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>

</ListBox>




MainWindow.xaml.cs

namespace Practice_1
{

public partial class MainWindow : Window
{
public List<AuctionItem> AuctionItemObjects;

public MainWindow()
{
AuctionItemObjects = new List<AuctionItem>()
{
new AuctionItem()
{
Description = "Inside C#. second edition",
StartDate = "8/8/2016",
StartPrice = "1000",
Category = "Books",
OwnerName = "Chummi",
MemberSince = "2004",
OwnerRating = "15",
SpecialFeatures = "Color",
CurrentPrice = "10"

},
new AuctionItem()
{
Description = "Laptop - only 1 year old",
StartDate = "8/9/2016",
StartPrice = "100",
Category = "Electronics",
OwnerName = "Mark",
MemberSince = "2005",
OwnerRating = "10",
SpecialFeatures = "Highlight"
},
new AuctionItem()
{
Description = "TV Drama Series",
StartDate = "8/10/2016",
StartPrice = "400",
Category = "DVDs",
OwnerName = "Chuhaan",
MemberSince = "2006",
OwnerRating = "5",
SpecialFeatures = ""
},
new AuctionItem()
{
Description = "My DVD Collection",
StartDate = "8/11/2016",
StartPrice = "5000",
Category = "DVDs",
OwnerName = "Charsi",
MemberSince = "2008",
OwnerRating = "35",
SpecialFeatures = "Highlight"
},

};
InitializeComponent();
DataContext = this;
}

public class AuctionItem
{

public string Description { get; set; }

public string StartPrice { get; set; }

public string StartDate { get; set; }

public string Category { get; set; }

public string SpecialFeatures { get; set; }

public string OwnerName { get; set; }

public string OwnerRating { get; set; }

public string MemberSince { get; set; }

public string CurrentPrice { get; set; }
}

}

}

Answer

You need to bind your ListBox to your collection.

Something like:

<ListBox Width="Auto" Height="Auto" ItemsSource="{Binding AuctionItemObjects}">

Otherwise how does it know what you want in your ListBox?

Note a couple of things however:

  • Usually it's better to have a model and a view model. The view model would be the DataContext for your view (which is your window). This is the MVVM pattern and you should learn it.
  • Your List<T> won't update your ListBox when you add or remove items from the underlying list. This is because it doesn't implement INotifyCollectionChanged, if you need that, consider ObservableCollection<T>
  • None of the properties in your AuctionItem will update the UI when they change either because your are not implementing INotifyPropertyChanged
  • AuctionItemObjects needs to be a property, not a field. WPF won't bind fields, so public List<AuctionItem> AuctionItemObjects {get; set; }
Comments