WP7图像绑定

发布于 2024-10-09 13:43:03 字数 1218 浏览 0 评论 0原文

我有一个包含列表框的 Windows Phone 7 应用程序。我希望列表框中的每个项目都有一个特定的图像。

<ListBox Height="606" HorizontalAlignment="Left" ScrollViewer.VerticalScrollBarVisibility="Visible"
                 Margin="20,20,0,0" 
                 Name="listBox1" VerticalAlignment="Top" Width="414" ItemsSource="{Binding SubList, Mode=OneWay}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Height="50" Width="50" 
                               VerticalAlignment="Top" Margin="0,10,8,0" Source="{Binding Image, Mode = OneWay}"/>
                        <StackPanel Orientation="Vertical">
                            <HyperlinkButton Content="{Binding Cathegory, Mode=OneWay}" NavigateUri="" HorizontalAlignment="Right">
                            </HyperlinkButton>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

与 Image 控件的 Source 属性绑定的属性是一个 Uri。 我的问题是图像没有显示在列表框中。 我尝试使用字符串而不是 Uri,但遇到了同样的问题。

我非常感谢任何帮助:)。

I have a Windows phone 7 application which contains a listbox. I want, for each item in the listbox to have a particular image.

<ListBox Height="606" HorizontalAlignment="Left" ScrollViewer.VerticalScrollBarVisibility="Visible"
                 Margin="20,20,0,0" 
                 Name="listBox1" VerticalAlignment="Top" Width="414" ItemsSource="{Binding SubList, Mode=OneWay}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Height="50" Width="50" 
                               VerticalAlignment="Top" Margin="0,10,8,0" Source="{Binding Image, Mode = OneWay}"/>
                        <StackPanel Orientation="Vertical">
                            <HyperlinkButton Content="{Binding Cathegory, Mode=OneWay}" NavigateUri="" HorizontalAlignment="Right">
                            </HyperlinkButton>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

And the property binded to the Source property of the Image control is an Uri.
My problem is that the image is not shown in the listbox.
I have tried using a string instead of the Uri but I get the same issue.

I kindly appreciate any help :).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

但可醉心 2024-10-16 13:43:03

这应该有效:

public class Item
{
    public int Number { get; set; }

    public Uri ImageUri { get; set; }

    public Item(int number, Uri imageUri)
    {
        Number = number;
        ImageUri = imageUri;
    }
}
And the datatemplate:


<ListBox x:Name="Items">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Number}"></TextBlock>
                <Image Source="{Binding ImageUri}"></Image>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

This should work:

public class Item
{
    public int Number { get; set; }

    public Uri ImageUri { get; set; }

    public Item(int number, Uri imageUri)
    {
        Number = number;
        ImageUri = imageUri;
    }
}
And the datatemplate:


<ListBox x:Name="Items">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Number}"></TextBlock>
                <Image Source="{Binding ImageUri}"></Image>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
抱猫软卧 2024-10-16 13:43:03

查看其他 SO 答案,我相信它包含您问题的答案:
图像 UriSource 和数据绑定

但它的基础知识是你需要转换BitmapImage 的 URI/字符串(通过 viewmodel 属性或转换器)

Check out this other SO answer, I believe it contains the answer to your question:
Image UriSource and Data Binding

But the basics of it is that you need to convert your URI/string to a BitmapImage (either via viewmodel property, or converter)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文