C# 在文本块中动态显示数据

C# 在文本块中动态显示数据,c#,windows-phone-7,textblock,C#,Windows Phone 7,Textblock,我只是想动态显示数据,所以我使用了C代码 但我的页面中没有显示任何内容 可能有什么问题???您需要以某种方式将它们添加到可视化树中。。。比如说 在您的XAML中: <Window x:Class="MyClass" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

我只是想动态显示数据,所以我使用了C代码

但我的页面中没有显示任何内容


可能有什么问题???

您需要以某种方式将它们添加到可视化树中。。。比如说

在您的XAML中:

<Window x:Class="MyClass"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Width="525"
        Height="350">
   <StackPanel x:Name="MainPanel" />
</Window>

您需要以某种方式将它们添加到可视化树中。。。比如说

在您的XAML中:

<Window x:Class="MyClass"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Width="525"
        Height="350">
   <StackPanel x:Name="MainPanel" />
</Window>
            foreach(var item in ContactPersons)
            {
                TextBlock tb = new TextBlock();
                tb.Margin = new Thickness(0,y_coordinateStart ,0,0);
                tb.Foreground = new SolidColorBrush(Colors.Green);
                tb.Visibility = Visibility.Visible;
                tb.Height = 30;
                tb.Width = 300;
                y_coordinateStart += 35;
                tb.Text = item.firsName;
                MainPanel.Children.Add(tb);
            }