如何根据解析的条目动态创建文本块

时间:2013-12-02 04:51:22

标签: c# windows-phone-7 windows-phone-8

我正在尝试解析一个Web API,基于我想要显示的所有条目,显示与单个帐户关联的所有银行帐户交易(所有条目的列表),并希望在动态创建的文本块中显示它。 类似这样: -

enter image description here

这是我的解析器类

public class Transaction
{
public string dec { get; set; }
public string amt { get; set; }
public string date { get; set; }
}

public class Loginresponse
{
public List<Transaction> transaction { get; set; }
}

这里是dec-描述,amt- Amount,我已成功解析了Web API。

<Grid Name="accountdetails" Margin="0,0,0,0" Background="#66ac22" HorizontalAlignment="Left" Width="456" Grid.ColumnSpan="2">
                    <TextBlock Grid.Row="0"  VerticalAlignment="Center" Margin="10,0,0,0" Text="No." Foreground="White"/>
                    <TextBlock  VerticalAlignment="Center" Margin="66,0,319,0" Text="Date." Foreground="White"/>
                    <TextBlock VerticalAlignment="Center" Margin="166,0,140,0" Text="Description" Foreground="White"/>
                    <TextBlock VerticalAlignment="Center" Margin="364,0,0,0" Text="Amount" Foreground="White"/>
                    <ItemsControl Name="accdetails" ItemsSource="{Binding Path=transaction}" Margin="0,10,0,-440">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" Background="Black">
                                    <TextBlock Text="{Binding dec}" Foreground="Black" />
                                    <TextBlock Text="{Binding amt}" Foreground="Black"/>
                                    <TextBlock Text="{Binding date}" Foreground="Black"/>
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                    </Grid>

我的班级档案: -

 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        try
        {
            IDictionary<string, string> parameters = this.NavigationContext.QueryString;
            if (parameters.ContainsKey("key"))
            {
                var root1 = JsonConvert.DeserializeObject<RootObject>(parameters["key"]);
                this.accdetails.DataContext = root1.loginresponse.transaction;
            }
        }
        catch (Exception)
        {

        }
    }

现在我无法弄清楚如何动态创建文本块并显示从解析器获得的所有这些细节。  任何形式的编写代码的指导,建议或正确方法都将受到赞赏。

 <Grid x:Name="LayoutRoot">
    <phone:Pivot Title="" Style="{StaticResource PivotStyle1}">

        <phone:PivotItem Header="transactions" FontSize="20">
            <Grid Margin="0,0,0,420" Background="#f9f9f9">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid Name="accountdetails" Margin="0,0,0,0" Background="#66ac22" HorizontalAlignment="Left" Width="456" Grid.ColumnSpan="2">
                    <TextBlock VerticalAlignment="Center" Margin="10,0,0,0" Text="No." Foreground="White"/>
                    <TextBlock VerticalAlignment="Center" Margin="66,0,319,0" Text="Date." Foreground="White"/>
                    <TextBlock VerticalAlignment="Center" Margin="166,0,140,0" Text="Description" Foreground="White"/>
                    <TextBlock VerticalAlignment="Center" Margin="364,0,0,0" Text="Amount" Foreground="White"/>
                    <ListBox x:Name="ListBox1" Margin="5,32,0,-340"
                           Width="450" HorizontalAlignment="Left" Padding="0" VerticalAlignment="Center"
                           ItemsSource="{Binding}" >
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" >
                                    <TextBlock Grid.Column="0" Text="{Binding no}" Foreground="Black" Margin="5,5,0,0" HorizontalAlignment="Left"/>
                                    <TextBlock Grid.Column="1" Text="{Binding date}" Foreground="Black" Margin="30,5,0,0" HorizontalAlignment="Left"  />
                                    <TextBlock Grid.Column="2" Text="{Binding dec}" Foreground="Black" Margin="40,5,0,0" HorizontalAlignment="Left"/>
                                    <TextBlock Grid.Column="3" Text="{Binding amt}" Foreground="Black" Margin="120,5,80,0" HorizontalAlignment="Left" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

现在我收到了文字列表,但我无法在网格中正确显示。

2 个答案:

答案 0 :(得分:2)

一个简单的选择是使用ItemsControl,它绑定到一个项目集合,并使用您指定的数据模板显示每个项目。

e.g。

<ItemsControl ItemsSource="{Binding Path=transaction}" >
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding dec}" />
                <TextBlock Text="{Binding amount}" />
                <TextBlock Text="{Binding date}" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

答案 1 :(得分:1)

Why don't you use a listbox instead?

Declare a Listbox in xaml like this with textblocks text binding your properties.

    <Listbox name ="lstBox">
        <Listbox.ItemTemplate>
            <Listbox>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding dec}" />
                    <TextBlock Text="{Binding amount}" />
                    <TextBlock Text="{Binding date}" />
                </StackPanel>
            </DataTemplate>
        </Listbox.ItemTemplate>
    </Listbox>

Now in cs file.

前25个参赛作品。

List<Type> newList=new List<Type>();

int count =your list.Count;

    if(count >25)
    {
      for (int i = 0; i < your list.Count; i++)
       {
        if(i<=24)
        {
          newList.Add(your list[i]};
        }
      }
   }

listBox.ItemSsource = newList;

试试这个。