数据在列表中不可见

时间:2013-07-01 07:11:03

标签: list data-binding windows-phone-8

在我的Windows Phone 8应用程序中,当用户单击按钮时,我正在调用Web服务并将结果输入List类型的变量中。 之后我试图将结果绑定到xaml页面中的列表。但数据不可见。

//Data Context object
class Company
    {      
    public string CompanyId { get; set; }

    public string CompanyName { get; set; }

    public string CityId { get; set; }

    public string Category { get; set; }

    public string CategoryId { get; set; }

    public string Address { get; set; }

    public string Phone1 { get; set; }

    public string Mobile1 { get; set; }

    public string Email { get; set; }

    public string Fax { get; set; }

    public string Website { get; set; }

    public string Profile { get; set; }

    public string Latitude { get; set; }

    public string Longitude { get; set; }

    public string Sponsored { get; set; }
}

在我的XAML页面中,我将上述结果绑定到List。

<Grid x:Name="ContentPanel" Grid.Row="3" Background="White" Margin="0,-3,0,0">
        <ListBox x:Name="companiesList"  
                 SelectionChanged="companiesList_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid x:Name="listItem">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="30"/>
                            <RowDefinition Height="30"/>
                            <RowDefinition Height="10"/>
                        </Grid.RowDefinitions>

                        <TextBlock x:Name="nameTextBlock" Grid.Row="0" Text="{Binding CompanyName}" Foreground="#FF501F6E" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" FontSize="28" MaxHeight="40" TextTrimming="WordEllipsis" Margin="5,0,0,5"/>

                        <TextBlock x:Name="addressTextBlock"  Grid.Row="1" Text="{Binding Address}" Foreground="#FF1F1F1F" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" FontSize="20" MaxHeight="30" TextTrimming="WordEllipsis" Margin="5,0,0,5"/>

                        <StackPanel x:Name="addressPanel" Grid.Row="2" Orientation="Horizontal" Margin="5,0,0,5">
                            <Image x:Name="phone" Stretch="Uniform" Margin="0,0,5,0" Height="25" Source="Images/list_phone.png"  />
                            <TextBlock x:Name="phoneTextBlock" Text="{Binding Phone1}" Foreground="#FF501F6E" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" FontSize="20" MaxHeight="30" TextTrimming="WordEllipsis"/>
                        </StackPanel>

                        <Image  x:Name="line" Grid.Row="3" Width="460" HorizontalAlignment="Center" Source="Images/separator.png"  />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

之后在代码隐藏文件中,我绑定如下。

  private void CompaniesPage_Loaded(object sender, RoutedEventArgs e)
    {

        if (isPageAlreadyLoaded == false)
        {                
            List<Company> companies = (List<Company>)DataContext;
            companiesList.ItemsSource = companies;
            isPageAlreadyLoaded = true;
        }
    }

调试时我在设置为列表之前检查了代码隐藏文件中的变量列出公司变量,并且我正确获取数据。 但是数据没有绑定到列表。我不知道为什么数据没有约束力。

现在测试我已经用静态文本替换了绑定。然后我运行应用程序,然后数据在页面中不可见但是列表项已更改我正在获取数据。这意味着我的xaml页面无法显示数据。那么请你告诉我我的xaml页面设计中出现了什么问题,如下所示。

<phone:PhoneApplicationPage
x:Class="STCDirectory.CompaniesPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True" Loaded="CompaniesPage_Loaded"
xmlns:my="clr-namespace:System.Windows.Controls;assembly=WindowsPhoneWatermarkTextBoxControl">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot">
    <Grid.Background>
        <ImageBrush Stretch="UniformToFill" ImageSource="/STCDirectory;component/Images/search_list_bg.png" />
    </Grid.Background>
    <Grid.RowDefinitions>
        <RowDefinition Height="80"/>
        <RowDefinition Height="80"/>
        <RowDefinition Height="80"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Orientation="Vertical" Margin="0,0,0,10" >
        <TextBlock x:Name="header" Text="STC Directory" HorizontalAlignment="Left" Margin="10,20,0,5"  Foreground="#FF501F6E" FontWeight="Bold" FontSize="35" />
    </StackPanel>
    <StackPanel x:Name="buttonsBar" Grid.Row="1" Orientation="Horizontal" Margin="0,10,0,0">
        <Button Content="Button" Height="70" HorizontalAlignment="Left" Margin="14,1,0,0" Name="button1" VerticalAlignment="Top" Width="160" />
        <Button Content="Button" Height="70" HorizontalAlignment="Left" Margin="130,1,0,0" Name="button2" VerticalAlignment="Top" Width="160" />
    </StackPanel>
    <StackPanel x:Name="searchBar" Grid.Row="2" Orientation="Horizontal" >
        <my:WatermarkTextBox Name="textBlock1" Width="400" Margin="-5,0,-10,0" WatermarkText="{Binding Path=LocalizedResources.SearchHint, Source={StaticResource LocalizedStrings}}" TextWrapping="Wrap" Foreground="Black" TextAlignment="Left" BorderBrush="{x:Null}" FontSize="25">
            <my:WatermarkTextBox.Background>
                <ImageBrush ImageSource="/STCDirectory;component/Images/search_box.png" />
            </my:WatermarkTextBox.Background>
        </my:WatermarkTextBox>
        <Button x:Name="serchButton" Style ="{StaticResource ButtonStyleIB}" VerticalAlignment="Center" Height="70"  Click="serach_button_clicked">
            <Image Source="/STCDirectory;component/Images/search.png" Stretch="Fill" />
        </Button>
    </StackPanel>
    <Grid x:Name="ContentPanel" Grid.Row="3" Background="White" Margin="0,-3,0,0">
        <ListBox x:Name="companiesList"  
                 SelectionChanged="companiesList_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid x:Name="listItem">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="30"/>
                            <RowDefinition Height="30"/>
                            <RowDefinition Height="10"/>
                        </Grid.RowDefinitions>

                        <TextBlock x:Name="nameTextBlock" Grid.Row="0" Text="Kentuc Fried Chicken(KFC)" Foreground="#FF501F6E" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" FontSize="28" MaxHeight="40" TextTrimming="WordEllipsis" Margin="5,0,0,5"/>

                        <TextBlock x:Name="addressTextBlock"  Grid.Row="1" Text="Al riyadh" Foreground="#FF1F1F1F" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" FontSize="20" MaxHeight="30" TextTrimming="WordEllipsis" Margin="5,0,0,5"/>

                        <StackPanel x:Name="addressPanel" Grid.Row="2" Orientation="Horizontal" Margin="5,0,0,5">
                            <Image x:Name="phone" Stretch="Uniform" Margin="0,0,5,0" Height="25" Source="Images/list_phone.png"  />
                            <TextBlock x:Name="phoneTextBlock" Text="966123456" Foreground="#FF501F6E" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" FontSize="20" MaxHeight="30" TextTrimming="WordEllipsis"/>
                        </StackPanel>

                        <Image  x:Name="line" Grid.Row="3" Width="460" HorizontalAlignment="Center" Source="Images/separator.png"  />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Grid>

我很期待你的回复 谢谢, Basina

2 个答案:

答案 0 :(得分:0)

public MainPage()
    {
        this.InitializeComponent();
        List<Company> companies = new List<Company>();
        Company comp = new Company();
        comp.CompanyName = "hellocpmp1";
        companies.Add(comp);
        companies.Add(comp);
        companies.Add(comp);
        companies.Add(comp);
        companies.Add(comp);

        companies.Add(comp);
        companies.Add(comp);

        companies.Add(comp);

        companies.Add(comp);
        companies.Add(comp);
        companies.Add(comp);



        companiesList.ItemsSource = companies;
    }

我已经尝试过如上所述并且工作正常,因此在您的情况下,如果没有来自Web服务的数据,则可能会出现问题。

答案 1 :(得分:0)

<phone:PhoneApplicationPage
x:Class="PhoneApp1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkit="clr-namespace:Coding4Fun.Toolkit.Controls;assembly=Coding4Fun.Toolkit.Controls"
mc:Ignorable="d"



shell:SystemTray.IsVisible="True">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="ContentPanel"  Background="Transparent" >
    <ListBox x:Name="companiesList"  ItemsSource="{Binding companies}"
             SelectionChanged="companiesList_SelectionChanged_2">
        <ListBox.ItemTemplate>
            <DataTemplate>

                <Grid x:Name="listItem">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="40"/>
                        <RowDefinition Height="30"/>
                        <RowDefinition Height="30"/>
                        <RowDefinition Height="10"/>
                    </Grid.RowDefinitions>

                    <TextBlock x:Name="nameTextBlock" Grid.Row="0" Text="{Binding CompanyName}" Foreground="#FF501F6E" />

                    <TextBlock x:Name="addressTextBlock"  Grid.Row="1" Text="Al riyadh" Foreground="#FF1F1F1F" />

                    <StackPanel x:Name="addressPanel" Grid.Row="2" Orientation="Horizontal" Margin="5,0,0,5">
                        <Image x:Name="phone" Stretch="Uniform" Margin="0,0,5,0" Height="25" Source="Images/list_phone.png"  />
                        <TextBlock x:Name="phoneTextBlock" Text="966123456" Foreground="#FF501F6E" />
                    </StackPanel>

                    <Image  x:Name="line" Grid.Row="3" Width="460" HorizontalAlignment="Center" Source="Images/separator.png"  />
                </Grid>



            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

我发现你的问题是你的文本块的样式部分有问题所以我已经删除它。我已经把一些工作代码与绑定以及硬编码文本放在一起。告诉我你的问题是否得到解决。