[英]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.. 我想在用户按下Button时向ListBox添加一个文本列表。每个ListItem包含绑定数据的TextBlock
。
But the TextBlock
is not showing the text! 但TextBlock
没有显示文本! 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.. 谢谢..
Your class Data
needs to be public else it would have private
access specifier by default.. 您的类Data
需要是公共的,否则它默认具有private
访问说明符。
So it should be 所以它应该是
public class Data.....
Everything else seems to be ok.. 其他一切似乎都没问题..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.