NAMO
NAMO

Reputation: 273

TextBlock doesn't show Text even after Binding

I wanted to add a list of text to ListBox as soon as the user press the Button..Each ListItem contains TextBlock to which am binding the data..

But the TextBlock is not showing the text! Though I could see the Background color of each Item being inserted!

<StackPanel>
    <Button Content="CLICK" Click="Button_Click"></Button>
    <ListBox x:Name="dataList" Foreground="Red" Background="Blue">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Feed}" FontSize="28"></TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
        </ListBox>
</StackPanel> 

My code behind looks like

public partial class MainPage : UserControl
{
    ObservableCollection<Data> data;
    public MainPage()
    {
        InitializeComponent();
        data = new ObservableCollection<Data>();
        dataList.ItemsSource = data;
    }
    class Data :INotifyPropertyChanged
    {
        public Data(String s)
        {
            Feed = s;
        }
        private string _feed;
        public String Feed
        {
            get { return _feed; }
            set { _feed = value; NotifyPropertyChanged("Feed"); }
        }
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        data.Add(new Data("News1"));
        data.Add(new Data("News2"));
        data.Add(new Data("News2"));
    }

}

Thanks..

Upvotes: 3

Views: 1060

Answers (1)

Anirudha
Anirudha

Reputation: 32797

Your class Data needs to be public else it would have private access specifier by default..

So it should be

public class Data.....

Everything else seems to be ok..

Upvotes: 4

Related Questions