【问题标题】:Binding List<List<string>> to ItemsControl将 List<List<string>> 绑定到 ItemsControl
【发布时间】:2013-09-01 17:29:54
【问题描述】:

我正在创建一个 Windows 应用商店应用程序。我想创建一个类似结构的表,所以我使用了 ItemsControl。我需要在给定的 JSON 下方显示,它是表中字符串列表的列表。我从here 尝试了一些东西,但我得到了重复的输出。

JSON

{
   head:[
      [
         "Your purchase",
         "Short number",
         "SMS text",
         "Price",
         "Operator"
      ]
   ],
   body:[
      [
         "10",
         "28000",
         "PAY 2p+2508812",
         "23.20 MXN",
         "TELCEL"
      ],
      [
         "10",
         "28000",
         "PAY 2p+2508812",
         "23.20 MXN",
         "MOVISTAR"
      ],
      [
         "6",
         "53035",
         "dam 10169 2508812",
         "15.08 MXN",
         "IUSACELL"
      ],
      [
         "10",
         "28000",
         "PAY 2p+2508812",
         "23.20 MXN",
         "Nextel"
      ],
      [
         "10",
         "28000",
         "PAY 2p+2508812",
         "23.20 MXN",
         "Lusacell"
      ]
   ]
}

模型类

public class SmsTable
{
    [JsonProperty("head")]
    public List<List<string>> Headers { get; set; }

    [JsonProperty("body")]
    public List<List<string>> RowValues { get; set; }
}

XAML

<ItemsControl x:Name="icOuter" Grid.Row="1" ItemsSource="{Binding RowValues}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ItemsControl x:Name="icInner" ItemsSource="{Binding}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid HorizontalAlignment="Left">
                            <StackPanel Orientation="Horizontal" Background="White" HorizontalAlignment="Left">
                                <Border Style="{StaticResource BorderCell}">
                                    <TextBlock Text="{Binding Items[0], ElementName=icInner}" Style="{StaticResource TextBlockCell}" Width="180"/>
                                </Border>
                                <Border Style="{StaticResource BorderCell}">
                                    <TextBlock Text="{Binding Items[1], ElementName=icInner}" Style="{StaticResource TextBlockCell}" Width="180"/>
                                </Border>
                                <Border Style="{StaticResource BorderCell}">
                                    <TextBlock Text="{Binding Items[2], ElementName=icInner}" Style="{StaticResource TextBlockCell}" Width="245"/>
                                </Border>
                                <Border Style="{StaticResource BorderCell}">
                                    <TextBlock Text="{Binding Items[3], ElementName=icInner}" Style="{StaticResource TextBlockCell}" Width="125"/>
                                </Border>
                                <Border Style="{StaticResource BorderCell}">
                                    <TextBlock Text="{Binding Items[4], ElementName=icInner}" Style="{StaticResource TextBlockCell}" Width="125"/>
                                </Border>
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

预期输出

实际输出

【问题讨论】:

    标签: json xaml windows-8 windows-runtime microsoft-metro


    【解决方案1】:

    您有一些需要更改的地方。

    1. 您只需要一个ItemsControl。它是用于遍历List&lt;&gt; 中的项目的外部ItemsControl。在这种情况下,您有一个List&lt;List&lt;string&gt;&gt;。 RowsData 中的每一项都是List&lt;string&gt;。
    2. 由于您对循环遍历内部List&lt;string&gt; 的所有项目不感兴趣,您将删除内部ItemsControl 并调整Binding. 绑定到索引器的语法是在C# 中指定索引符号:[#]。 List&lt;string&gt; 没有可用于访问索引值的属性(请记住,所有绑定都必须是属性或使用转换器)。在这种情况下,有一种特殊的属性路径语法可以完全满足您的需求。

    进行这些更改:

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <ItemsControl x:Name="icOuter" Grid.Row="1" ItemsSource="{Binding RowValues}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>                    
                    <Grid HorizontalAlignment="Left">                        
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                            <Border >
                                <TextBlock Text="{Binding [0]}" Width="180"/>
                            </Border>
                            <Border >
                                <TextBlock Text="{Binding [1]}" Width="180"/>
                            </Border>
                            <Border >
                                <TextBlock Text="{Binding [2]}" Width="245"/>
                            </Border>
                            <Border >
                                <TextBlock Text="{Binding [3]}" Width="125"/>
                            </Border>
                            <Border >
                                <TextBlock Text="{Binding [4]}" Width="125"/>
                            </Border>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
    

    结果:

    【讨论】:

    • 是的!有效。你能解释一下你在DataTemplate中所做的绑定吗?
    猜你喜欢
    • 1970-01-01
    • 2013-12-07
    • 1970-01-01
    • 2011-02-10
    • 2014-06-03
    • 2013-05-03
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多