即使在绑定后,TextBlock也不会显示文本

时间:2013-10-01 12:17:29

标签: c# wpf silverlight xaml

我想在用户按下按钮后立即向ListBox添加文本列表。每个ListItem包含TextBlock,绑定数据..

TextBlock 显示文字!虽然我可以看到插入每个项目的背景颜色!

<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> 

我的代码背后看起来像

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"));
    }

}

谢谢..

1 个答案:

答案 0 :(得分:4)

您的班级Data需要公开,否则默认会有private访问说明符。

所以它应该是

public class Data.....

其他一切似乎都没问题。