将嵌套列表视图绑定到类

时间:2016-04-14 09:52:28

标签: c# xaml listview data-binding

这是我的班级:

 public class ZuResults
{
    public Direct_Matches[] direct_matches { get; set; }
    public int found { get; set; }
    public string query { get; set; }

    public ZuResults()
    {

    }
}

public class Direct_Matches
{
    public object[] data { get; set; }
    public string[] headwords { get; set; }
    public Metadata metadata { get; set; }
}

public class Metadata
{
    public string pos { get; set; }
    public string lang { get; set; }
}

这是我试图绑定此类的ListView:

<ListView Grid.Row="0" Margin="0,50,0,0" x:Name="listZuResults"  
                     SelectionMode="Single" Background="White"
                     ScrollViewer.VerticalScrollBarVisibility="Auto" ItemsSource="{Binding}" 
                     BorderThickness="0,0,1,0" BorderBrush="DarkGray">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid BorderThickness="1,1,1,2" BorderBrush="LightGray" Width="350">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <StackPanel VerticalAlignment="Center">
                        <TextBlock Margin="0,0,0,0" Grid.Row="0" x:Name="Query" Foreground="{Binding FontColor}" TextWrapping="Wrap" Text="Query" FontSize="15" FontWeight="Bold"/>
                        <TextBlock Margin="0,0,0,0" Grid.Row="0" x:Name="txtQuery" Foreground="{Binding FontColor}" TextWrapping="Wrap" Text="{Binding query, Mode=TwoWay}" FontSize="12"/>
                        <TextBlock Margin="0,0,0,0" Grid.Row="0" x:Name="Found" Foreground="{Binding FontColor}" TextWrapping="Wrap" Text="Results found" FontSize="15" FontWeight="Bold"/>
                        <TextBlock Margin="0,0,0,0" Grid.Row="0" x:Name="txtFound" Foreground="{Binding FontColor}" TextWrapping="Wrap" Text="{Binding found, Mode=TwoWay}" FontSize="12"/>
                        <ListView ItemsSource="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},Path=direct_matches[1]}">
                            <DataTemplate>
                                <Grid BorderThickness="1,1,1,2" BorderBrush="LightGray" Width="350">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <StackPanel VerticalAlignment="Center">
                                        <TextBlock Margin="0,0,0,0" Grid.Row="0" x:Name="Metadata" Foreground="{Binding FontColor}" TextWrapping="Wrap" Text="Metadata" FontSize="15" FontWeight="Bold"/>
                                        <ListView ItemsSource="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},Path=metadata}">
                                            <TextBlock Margin="0,0,0,0" Grid.Row="0" x:Name="Pos" Foreground="{Binding FontColor}" TextWrapping="Wrap" Text="Pos" FontSize="15" FontWeight="Bold"/>
                                            <TextBlock Margin="0,0,0,0" Grid.Row="0" x:Name="txtPos" Foreground="{Binding FontColor}" TextWrapping="Wrap" Text="{Binding pos}" FontSize="12"/>
                                        </ListView>
                                    </StackPanel>
                                </Grid>
                            </DataTemplate>
                        </ListView>
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

当我在MainPage.xaml代码隐藏中设置listZuResults的ItemsSource时,我调试时实际出现的唯一值是txtQuery和txtFound。如何显示其余数据,例如&#39; pos&#39;和&#39; lang&#39;?

0 个答案:

没有答案