列表框项目未显示

时间:2012-03-25 18:10:57

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

所以我正在开发一个Windows Phone 7应用程序,我遇到了问题,通常在我的其他WPF / WinForm应用程序中这个代码可以工作但是在Wphone 7上我收到了问题,我创建了数据类:< / p>

public class AlarmTemplate
{
    public string Name { get; set; }
    public string Time { get; set; }
    public BitmapImage Activated { get; set; }

    public AlarmTemplate(string name, string time, string activated)
    {
        Name = name;
        Time = time;
        Activated = new BitmapImage
                        {UriSource = new Uri("Images/alarm_" + activated + ".png", UriKind.RelativeOrAbsolute)};
    }

}

接下来的事情是读爸爸,我也试过硬编码数据并且无法正常工作:

private List<AlarmTemplate> _templateList = new List<AlarmTemplate>();

private void PopulateList()
    {
        using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (!storage.FileExists("file.txt"))
                return;

            using (var reader = new BinaryReader(storage.OpenFile("file.txt", FileMode.Open)))
            {
                var s = reader.ReadInt32();
                for (var i = 0; i < s; i++)
                {
                    _templateList.Add(new AlarmTemplate(reader.ReadString(), reader.ReadString(),
                                                        reader.ReadString()));
                }
            }
        }
        lbAlarms.ItemsSource = _templateList;
}

这是xaml:

<ListBox Height="176.135" HorizontalAlignment="Left" Margin="0,567.164,0,0" Name="lbAlarms" VerticalAlignment="Top" Width="456" Foreground="#FFFFC7C7" ItemsSource="{Binding}" Background="Transparent" AllowDrop="False" BorderThickness="1" BorderBrush="#00900707" Grid.Row="1" Hold="lbAlarms_Hold">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Grid.Row="1" Height="52" Orientation="Horizontal" Name="spList" VerticalAlignment="Top" Width="480" Margin="0,329,0,0" UseLayoutRounding="False">
                    <Image Height="52" Name="imTStatus" Stretch="Uniform" Width="73" Margin="10,0,0,0" UseLayoutRounding="False" Source="{Binding Activated}" />
                    <StackPanel Height="52" Name="spHolder" Width="300" Margin="10,0,0,0" VerticalAlignment="Stretch" HorizontalAlignment="Left" UseLayoutRounding="False">
                        <TextBlock Height="26" Name="tbTTime" Text="{Binding Time}" Foreground="Black" FontFamily=".\Fonts\Nokia.ttf#Nokia" TextAlignment="Left" FontWeight="Bold" Width="230" FontSize="24" HorizontalAlignment="Left" UseLayoutRounding="False" />
                        <TextBlock Height="26" Name="tbTName" Text="{Binding Name}"  Foreground="Black" FontFamily=".\Fonts\Nokia.ttf#Nokia" HorizontalAlignment="Left" Width="297" />
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

2 个答案:

答案 0 :(得分:2)

您的代码正常工作(我注意到右侧有滚动条),但您的文字颜色为黑色,因此不太明显。

摆脱TextBlock颜色 Foreground="Black":)

请注意,您的保证金意味着您的列表框在页面底部非常小,少于1个项目高,所以您可能也想要更改它。

答案 1 :(得分:0)

private List<AlarmTemplate> _templateList = new List<AlarmTemplate>();
public List<AlarmTemplate> TemplateList 
{
     get { return _templateList; }
     set { _templateList = value; }
}

并将绑定设置为TemplateList。