On the MainWindow of my WPF application I have a simple listBox that is databound to an ObservableCollection. The ObservableCollection contains members of a simple "Product" class with one string-property. The goal is to show the text that is stored in the "PName" property of all Products of the ObservableCollection.
The MainWindow.xaml:
<Window x:Class="BindingCheck.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:local="clr-namespace:BindingCheck"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox Name="listBox1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Label FontSize="26" Content="{Binding Path=PName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
The MainWindow.Xaml.Cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
namespace BindingCheck
{
public class Product
{
private string pName;
public string PName
{
get { return pName; }
}
public Product(string pname)
{
pName = pname;
}
}
public partial class MainWindow : Window
{
private ObservableCollection<Product> products;
public MainWindow()
{
InitializeComponent();
products = new ObservableCollection<Product>();
products.Add(new Product("Toaster"));
products.Add(new Product("Big_Toaster"));
products.Add(new Product("Very_Big_Toaster"));
this.DataContext = products;
}
}
}
And now my question: Why is only every second '_' character shown in the listBox output? The Output-Items should be: "Toaster", "Big_Toaster" and "Very_Big_Toaster" but however I get another output:
Output-Items in listBox: Toaster, BigToaster, VeryBig_Toaster