简体   繁体   中英

ListBox in wp7 doesn't work for me

I'm trying to present a listview in wp7 and for some reason it doesn't seem to work

my xaml

            <!--ContentPanel - place additional content here-->
        <StackPanel x:Name="ContentPanel2" Grid.Row="1" Margin="12,0,12,0">
            <ListBox x:Name="list">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="5">
                        <Image Source="{Binding ImageUri}" Stretch="None"/>
                        <TextBlock Text="{Binding Text}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>

    </Grid>

my c# code

    public class list
{
    public string title { get; set; }
    public string imageSource { get; set; }
}

and

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        List<list> dataSources = new List<list>();
        dataSources.Add(new list() { title = "Shacharit", imageSource = "Images/shacharit.png" });
        dataSources.Add(new list() { title = "Mincha", imageSource = "Images/mincha.png" });
        dataSources.Add(new list() { title = "Arvit", imageSource = "Images/arvit.png" });
        dataSources.Add(new list() { title = "Birkat HaMazon", imageSource = "Images/shacharit.png" });
        list.ItemsSource = dataSources;
    }

Thanks in advance

Try the below, change the bindings of image and text block to bind to the strings you have declared at present you are trying to bind to ImageURI and Text and they don't exist in any of your code.

           <!--ContentPanel - place additional content here-->
    <StackPanel x:Name="ContentPanel2" Grid.Row="1" Margin="12,0,12,0">
        <ListBox x:Name="list" Da>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="5">
                    <Image Source="{Binding imageSource }" Stretch="None"/>
                    <TextBlock Text="{Binding title}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>

</Grid>

To Clarify Jon D's answer, you are creating data objects with attributes of "imagePath" and "title" in your code behind

new list() { title = "Shacharit", imageSource = "Images/shacharit.png" };

but trying to bing to properties called "ImageUri" and "Text".

In your output window in VS you should see these binding errors show up.

The following 2 lines (where you are doinng the binding in the XAML) should fix things up for you...

<Image Source="{Binding imageSource }" Stretch="None"/>
<TextBlock Text="{Binding title}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM