动态生成文本块

时间:2015-03-04 10:03:42

标签: windows-phone-8

我正在使用Windows Phone Panorama App模板并使用LongListSelector允许用户单击并查看Long列表中列出的项目的详细信息。 DetailsPage.xaml中提供了项目的详细信息。 我的代码工作正常,即用户可以跳转到DetailsPage.xaml上的正确项目,并且内容对他们可见。

                

                <!--new text block -->
                <TextBlock Text="{Binding Term_1}" Style="{StaticResource Textblock}" FontWeight="Bold" Foreground="{StaticResource PhoneAccentBrush}"/>
                <TextBlock Text="{Binding Definition_1}" Style="{StaticResource Textblock}"  Margin="10,0,0,20" TextWrapping="Wrap"/>

                <TextBlock Text="{Binding Term_2}" Style="{StaticResource Textblock}" FontWeight="Bold" Foreground="{StaticResource PhoneAccentBrush}"/>
                <TextBlock Text="{Binding Definition_2}" Style="{StaticResource Textblock}" Margin="10,0,0,20"/>

                <TextBlock Text="{Binding Term_3}" Style="{StaticResource Textblock}" FontWeight="Bold" Foreground="{StaticResource PhoneAccentBrush}"/>
                <TextBlock Text="{Binding Definition_3}" Style="{StaticResource Textblock}" Margin="10,0,0,20"/>

                <TextBlock Text="{Binding Term_4}" Style="{StaticResource Textblock}" FontWeight="Bold" Foreground="{StaticResource PhoneAccentBrush}"/>
                <TextBlock Text="{Binding Definition_4}" Style="{StaticResource Textblock}" Margin="10,0,0,20"/>

                <TextBlock Text="{Binding Term_5}" Style="{StaticResource Textblock}" FontWeight="Bold" Foreground="{StaticResource PhoneAccentBrush}"/>
                <TextBlock Text="{Binding Definition_5}" Style="{StaticResource Textblock}" Margin="10,0,0,20"/>

                <TextBlock Text="{Binding Term_6}" Style="{StaticResource Textblock}" FontWeight="Bold" Foreground="{StaticResource PhoneAccentBrush}"/>
                <TextBlock Text="{Binding Definition_6}" Style="{StaticResource Textblock}" Margin="10,0,0,20"/>


我的问题是如何显示DetailsPage.xaml上的项目。例如,如果Longlistselector上的选项1有4个项目,而选项2有6个项目,则选项2看起来很好,但是选项1最后有很多空格(可能来自第5项和第6项的无数据) )。如果有另一个选项3只有1个项目,整个DetailsPage在显示单个项目后包含大量空格。我在一些选项中有超过100个项目,而在DetailsPage末尾的不需要的额外空格简直令人讨厌。我想知道是否有动态生成格式化的Textblocks以及DetailsPage中的绑定资源,以便可以删除多余的空格?如果没有,我如何只显示给定选项所需的文本块?

更新:这是在textblocks中加载数据的方式:

public void LoadData()
        {


            this.Items.Add(new ItemViewModel()

{
    ID = "0",
    Term_1 = "This is a term1",
    Definition_1 = "This definition1", 

Term_2 = "This is a term2",
        Definition_2 = "This definition2",

Term_3 = "This is a term3",
        Definition_3 = "This definition3",

Term_4 = "This is a term4",
        Definition_4 = "This definition4",

Term_5 = "This is a term5",
        Definition_5 = "This definition5",

});

this.Items.Add(new ItemViewModel()

{
    ID = "1",`enter code here`
    Term_1 = "This is a term1 for ID 1",
    Definition_1 = "This definition1 for ID 1"

});
this.Items.Add(new ItemViewModel()

{
    ID = "2",
    Term_1 = "This is a term1 for ID 2",
    Definition_1 = "This definition1 for ID 2"

});
this.Items.Add(new ItemViewModel()

{
    ID = "3",
    Term_1 = "This is a term1 for ID 3",
    Definition_1 = "This definition1 for ID 3"

});
this.Items.Add(new ItemViewModel()

{
    ID = "4",
    Term_1 = "This is a term1 for ID 4",
    Definition_1 = "This definition1 for ID 4"

});
this.Items.Add(new ItemViewModel()

{
    ID = "5",
    Term_1 = "This is a term1 for ID 5",
    Definition_1 = "This definition1 for ID 5"

});

this.Items.Add(new ItemViewModel()

{
    ID = "6",
    Term_1 = "This is a term1 for ID 6",
    Definition_1 = "This definition1 for ID 6"

});

如果我选择任何文本块,我将获得Texblock各自ID中列出的术语和定义。如果我选择第一个Textblock,我将获得条款/定义1,2,3,4,5。如果我选​​择另一个文本块,我将只得到一个术语/定义。

这就是输出中的确切方式。问题是,如果没有要为该ID显示的术语,则术语2,3,4,5和6的Textblock会引入空格。因此,空文本块正在占用屏幕上的空间。我希望Detailspage只呈现适用于ID的TextBlocks,然后停止为该ID的剩余块创建它们。

1 个答案:

答案 0 :(得分:0)

而不是定义&#34; Term_1&#34;通过&#34; Term_2&#34;作为ItemViewModel上的属性,在ItemViewModel上声明一个List属性(我假设这里有类型字符串):

public class ItemViewModel
{
    // ... other properties
    public List<string> Terms
    {
       get; set; // Implementation ommited
    }
}

然后,ItemViewModel实例的创建将变为类似

ItemViewModel viewModelItem= new ItemViewModel() { Id = "1", Definition_1 = "..." };
viewModelItem.Terms.Add("Term1 for Id1");
viewModelItem.Terms.Add("Term2 for Id1");
viewModelItem.Terms.Add("Term3 for Id1");
// ...as many terms as you have...
this.Items.Add(viewModelItem);

在您的详细信息页面中,我假设显示了一个ItemViewModelLongListSelector中选定的一个),您可以在XAML中添加ListView(或类似的东西)并绑定它到ItemViewModel类的条款属性:

<!-- Assumes an `ItemViewModel` instance is the `DataContext` for the ListView! -->
<ListView ItemsSource={Binding Terms}>
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text={Binding}/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

ListView会根据需要生成尽可能多的ListViewItems