문제

I just want to display data dynamically,so i used the C# code,

TextBlock[] Cp = new TextBlock[ContactPersons.Count];
                int i=0;
                foreach(var item in ContactPersons)
                {
                    Cp[i] = new TextBlock();
                    Cp[i].Margin = new Thickness(0,y_coordinateStart ,0,0);
                    Cp[i].Foreground = new SolidColorBrush(Colors.Green);
                    Cp[i].Visibility = Visibility.Visible;
                    Cp[i].Height = 30;
                    Cp[i].Width = 300;
                    y_coordinateStart += 35;
                    Cp[i].Text = item.firsName;
                    i++;
                }

But nothing appears in my page.

What could be the problem??

도움이 되었습니까?

해결책

You need to add them to the visual tree somehow... For instance

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

In your code:

            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);
            }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top