如何在Windows Phone 7应用程序的列表框中显示完整数据

时间:2014-03-10 09:24:57

标签: c# xaml windows-phone-7 listbox

我正在为Windows Phone 7构建一个应用程序,我有一个列表框,我在其中显示来自webservice的数据。我想在列表框中显示完整的数据。我正在使用textwrapping来换行,但它仍然没有显示数据。不显示超出屏幕的数据。此外,我希望如果有人点击列表框中的项目,它将导航到新页面。我可以使用列表框中的按钮来完成它,但我不想使用按钮。请查看我的xaml并尝试解决我的问题。

的Xaml:

<ListBox Name="CityList" BorderThickness="0" 
         Height="650" VerticalAlignment="Bottom" 
         SelectionChanged="CityList_SelectionChanged" Foreground="Black" 
         Background="AntiqueWhite" Grid.Row="1">

<ListBox.ItemTemplate>
 <DataTemplate>
  <!--<Button IsHitTestVisible="False" BorderThickness="0">
      <Button.Content>-->

 <ScrollViewer HorizontalScrollBarVisibility="Disabled"
               VerticalScrollBarVisibility="Disabled"
               Height="80" Width="800">

 <StackPanel Orientation="Horizontal" 
             Margin="0,0,10,10"
             Background="AntiqueWhite" 
             Width="2000">

 <Image Source="{Binding ImageBind }" 
        HorizontalAlignment="Stretch" 
        VerticalAlignment="Stretch" 
        Margin="0,0,20,10" Height="100" 
        Width="145" />

 <StackPanel Orientation="Vertical">
    <StackPanel Orientation="Horizontal">

 <TextBlock Text="{Binding city_name}"
            Foreground="Red" 
            FontFamily="Verdana" />

 <TextBlock Text=", " Foreground="Red" FontFamily="Verdana" />
 <TextBlock Text="{Binding state}" Foreground="Red" 
            FontFamily="Verdana" />

 </StackPanel>

 <TextBlock Text="{Binding Path=city_description}" 
 TextWrapping="Wrap" Foreground="Black" FontFamily="Verdana" Margin="10,0,10,10">
 </TextBlock>

 </StackPanel>
    </StackPanel>
 </ScrollViewer>

 <!--</Button.Content>
     </Button>-->

</DataTemplate>
  </ListBox.ItemTemplate>

</ListBox>

评论部分是一个按钮,当我使用时我可以导航但是可以不使用按钮进行导航

1 个答案:

答案 0 :(得分:1)

我认为你的结构看起来有点复杂:下面的代码会起作用吗?

<ListBox Name="CityList" BorderThickness="0" 
             Height="600" VerticalAlignment="Bottom" 

              Foreground="Black" 
              Background="AntiqueWhite" Grid.Row="1">

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <!--      <Button IsHitTestVisible="False" BorderThickness="0">
        <Button.Content>-->

                    <Grid Tap="ShowState">

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="200" />
                            <ColumnDefinition Width="10" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>

                        <Image Grid.Column="0" Source="http://static.bbci.co.uk/frameworks/barlesque/2.60.3/orb/4/img/bbc-blocks-dark.png" 
                           HorizontalAlignment="Stretch" 
                           VerticalAlignment="Stretch" 
                           Margin="0,0,20,10" Height="100" Width="145" />
                        <StackPanel Grid.Column="2">
                            <TextBlock Text="{Binding city_name}"
                                       Foreground="Red" 
                                       FontFamily="Verdana" TextWrapping="Wrap" />

                            <TextBlock Text=", " Foreground="Red" FontFamily="Verdana" />
                            <TextBlock Text="{Binding state}" Foreground="Red" TextWrapping="Wrap"
                          FontFamily="Verdana" />

                            <TextBlock Text="{Binding city_description}" 
                                   TextWrapping="Wrap" Foreground="Black" FontFamily="Verdana" Margin="10,0,10,10" />
                        </StackPanel>
                    </Grid>


                </DataTemplate>
            </ListBox.ItemTemplate>

        </ListBox>

假设你有

public class CityListData
{
    public string city_name { get; set; }
    public string state { get; set; }
    public string city_description { get; set; }
}

您的代码

private void ShowState(object sender, GestureEventArgs e)
    {
        var control = sender as Grid;

        if (control != null)
        {
            var entity = (CityListData) control.DataContext;
            MessageBox.Show("You clicked on the state " + entity.city_name);
        }
    }