Search code examples
c#wpfxamlbindingtextblock

Binding TextBlock To Observable Collection


I am trying to bind a textblock to a item in a observable collection. Below is the class and the property for the collection. Am I binding correctly? Thanks!

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

private ObservableCollection<ListStuff> mListStuff = new ObservableCollection<ListStuff>();

public ObservableCollection<ListStuff> NameLists
        {
            get
            {
                return mListStuff;
            }
        }

Here is the Xaml if I bind to NameLists it says (collection)

        <Border Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" BorderThickness="0"                        
            <TextBlock Margin="0,5" FontSize="24" HorizontalAlignment="Stretch" Text="{Binding Path=Name"}/>                                                                        
          </Border>

Solution

  • Your "Name" property has to be public. You can bind only against public properties. Leaving out identifier from property name, means essentially that it becomes a private one, so WPF system can't find it, because it searches only for public properties using reflection.