我正在尝试创建一个读取RSS源的应用程序,并以WPF格式显示它们。为了分隔帖子我使用网格并添加2个RowDefinitions(一个用于标题,一个用于主题)。
目前我被困住了。我想在我刚刚定义的行中添加一些Textblocks,但我不知道是怎么回事,因为他们没有名字。有没有人有想法?
我现在有什么:
//defining Grid
Grid post = new Grid();
post.HorizontalAlignment = HorizontalAlignment.Left;
post.VerticalAlignment = VerticalAlignment.Top;
post.Margin = new Thickness(10);
post.ShowGridLines = true;
post.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = new GridLength(500)
});
//loop trough all RSS feeds
foreach (SyndicationItem item in feed.Items)
{
//add rows for title and Summary
post.RowDefinitions.Add(new RowDefinition()
{
Height = new GridLength(50),
});
post.RowDefinitions.Add(new RowDefinition()
{
Height = new GridLength(50)
});
//Grid.SetRow(TitleTextblock, 0);
//Grid.SetRow(SummaryTextblock, 1);
//fills textblocks
post.Children.Add(new TextBlock()
{
Text = item.Title.Text
});
post.Children.Add(new TextBlock()
{
Text = item.Summary.Text
});
}
//show grid on window
this.Content = post;
导致: this
答案 0 :(得分:0)
你可以尝试
TextBlock textBlock = new TextBlock();
Grid.SetRow(textBlock, 0);
Grid.SetColumn(textBlock, 0);
希望这有助于你
答案 1 :(得分:0)
好吧,正确设置Definitions
似乎有问题:
//defining Grid
Grid post = new Grid();
post.HorizontalAlignment = HorizontalAlignment.Left;
post.VerticalAlignment = VerticalAlignment.Top;
post.Margin = new Thickness(10);
post.ShowGridLines = true;
post.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = new GridLength(500)
});
//add local field to keep track of what row you're on
int rowdeff = 0;
//loop trough all RSS feeds
foreach (SyndicationItem item in feed.Items)
{
//add rows for title and Summary
post.RowDefinitions.Add(new RowDefinition()
{
Height = new GridLength(50),
});
post.RowDefinitions.Add(new RowDefinition()
{
Height = new GridLength(50)
});
//create the textblocks
TextBlock TitleTextblock = new TextBlock;
TextBlock SummaryTextblock = new TextBlock;
//add content to textblocks
TitleTextBlock.Text = item.Title.Text;
SummaryTextblock.Text = item.Summary.Text;
//set definitions
Grid.SetColumn(TitleTextblock, 0);
Grid.SetColumn(SummaryTextblock, 1);
Grid.SetRow(TitleTextblock, rowdeff);
Grid.SetRow(SummaryTextblock, rowdeff);
//fills textblocks
post.Children.Add(TitleTextBlock);
post.Children.Add(SummaryTextblock);
//add next row
rowdeff++;
}
//show grid on window
this.Content = post;