简体   繁体   English

wp7中的ListBox对我不起作用

[英]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 我正在尝试在wp7中呈现一个列表视图,由于某种原因,它似乎不起作用

my xaml 我的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 我的C#代码

    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. 请尝试以下操作,将image和text块的绑定更改为绑定到您当前声明的字符串,您正在尝试绑定到ImageURI和Text,并且它们在您的任何代码中均不存在。

           <!--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 为了弄清乔恩·D的答案,您正在后面的代码中创建具有“ imagePath”和“ title”属性的数据对象

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

but trying to bing to properties called "ImageUri" and "Text". 但尝试使用“ ImageUri”和“ Text”属性。

In your output window in VS you should see these binding errors show up. 在VS的输出窗口中,您应该看到这些绑定错误出现。

The following 2 lines (where you are doinng the binding in the XAML) should fix things up for you... 以下两行(您在XAML中绑定的位置)应该可以为您解决问题……

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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