1

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

User95
  • 87
  • 1
  • 9

1 Answers1

0

See below code:

<StackPanel>
    <Label Content="_first_second" Target="{Binding ElementName=txtbox}"/>
    <TextBox Name="txtbox" Width="100" Height="50"/>
</StackPanel>

Now after the screen is Loaded press key 'f' any you'll see the Focus is set in the TextBox.

Cause the the content of Label is treated as AccessText for Label here and according to AccessText definition first '' char means the Accesskey to element and '' is not visible.

use below links for ref:

MSDN on AccessText

AccessText example

Kylo Ren
  • 8,551
  • 6
  • 41
  • 66
  • Thanks for reply! In my case is was much easier to replace the Label in the DataTemplate with a TextBlock (like swiszcz mentioned before). – User95 Feb 22 '16 at 09:27
  • @User95 yes. but your question was only why is that happening? that's why I didn't put a replacement XAML for your problme.. – Kylo Ren Feb 22 '16 at 10:01
  • aha ok. I misunderstood your answer. Then you're right ;) – User95 Feb 22 '16 at 12:00