如何将WinRT中的图像绑定到列表框

时间:2012-10-04 11:42:34

标签: vb.net xaml windows-8 windows-runtime

我是xaml编程的新手。我一直试图将多个图像绑定到列表框而没有运气。我能够看到文本而不是winrt app中的图像。以下是代码:

Imports Windows.Storage.Pickers
Imports Windows.Storage

Public NotInheritable Class MainPage
    Inherits Page

    Dim p As System.Uri

    ''' <summary>
    ''' Invoked when this page is about to be displayed in a Frame.
    ''' </summary>
    ''' <param name="e">Event data that describes how this page was reached.  The Parameter
    ''' property is typically used to configure the page.</param>
    Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)

    End Sub

Private Async Sub SelectFileName_Click(sender As Object, e As RoutedEventArgs) Handles _SelectFileName.Click

        Dim SelectedFileNameObject As New FileOpenPicker
        SelectedFileNameObject.FileTypeFilter.Add("*")

Dim SelectedFileName As IReadOnlyList(Of StorageFile) = Await SelectedFileNameObject.PickMultipleFilesAsync

        Dim a As New ObservableCollection(Of ImageLoc)
        For i As Integer = 0 To SelectedFileName.Count - 1

            p = New Uri(SelectedFileName.Item(i).Path.ToString, UriKind.RelativeOrAbsolute)

            a.Add(New ImageLoc() With {.ImageLocation = _SelectedFileName.Item(i).Path.ToString, .LineFour = p})

        Next
        ListName.ItemsSource = a

    End Sub

End class

Public Class ImageLoc
    Public location As String

    Property ImageLocation() As String
        Get
            Return location
        End Get
        Set(ByVal value As String)
            location = value
        End Set
    End Property
    Public b As Uri
    Public Property LineFour() As Uri
        Get
            Return b

        End Get
        Set(ByVal value As Uri)
            b = value
        End Set
    End Property
End Class

Xaml是:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel Height="auto" Width="auto" Orientation="Horizontal">
        <Button x:Name="SelectFileName" Width="100" Height="50" Content="Browse Files" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,40,0"/>
        <ListBox x:Name="ListName" Width="700" Height="auto">
            <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                      <TextBlock Text="{Binding ImageLocation}" Height="auto" Width="auto"/>
                        <Image Height="100" Width="100">
                            <Image.Source>
                                <BitmapImage UriSource="{Binding Path=LineFour}"/>
                            </Image.Source>
                        </Image>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</Grid>

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

首先,您实际上不必执行速记BitmapImage代码。 Image控件间谍端口将ImageSourceURI直接绑定到其Source属性。另外,你确定你传递的Uris格式正确吗?有时他们需要在前面有包名称前缀才能使它们正常工作。

答案 1 :(得分:0)

请参阅change image source programmatically

基本上:

<Image Margin="5" Source="{Binding BMImage}" Height="100"/>

BitmapImage bmImage;
public BitmapImage BMImage
{
    get
    {
        return bmImage;
    }
}

bmImage = new BitmapImage();
bmImage.UriSource = new Uri(new Uri(
     *your file path*, 
     *your image name*);

有关更多示例,请参阅我的博客here