【问题标题】:A simple image listview一个简单的图像列表视图
【发布时间】:2011-08-24 04:53:22
【问题描述】:

我正在尝试制作一个简单的字典绑定图像列表视图,其中键是filename,值是image path

下面是代码

<ListView Grid.Column="2" Grid.Row="3" Height="131" HorizontalAlignment="Left" Margin="6,9,0,0" Name="GenreListView1" VerticalAlignment="Top" Width="375">
    <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal"></WrapPanel>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    <ListView.ItemTemplate>
    <DataTemplate>
        <Image Source="{Binding}"></Image>
    </DataTemplate>
</ListView.ItemTemplate>
                    </ListView>

以下是运行时的代码:

    Dim maindir As DirectoryInfo = My.Computer.FileSystem.GetDirectoryInfo(CurDir() + "\Icons")
    GenreDictionary.Clear()
    For Each k As FileInfo In maindir.GetFiles()
        If k.Name.EndsWith(".png") Then
            GenreDictionary.Add(k.Name, k.FullName)
        End If
    Next

    'Load Icons to Genre View
    GenreListView1.ItemsSource = GenreDictionary.Values

能否请您指导我如何绑定图像。这是一个 Windows 应用程序。

【问题讨论】:

    标签: wpf listview


    【解决方案1】:

    你为什么要使用字典?为什么不只使用文件名列表?如果你这样做,那么你的&lt;Image Source="{Binding"} /&gt; 就可以了。您的代码将如下所示:

    Dim maindir As DirectoryInfo = My.Computer.FileSystem.GetDirectoryInfo(CurDir() + "\Icons")
    
    Dim fileList = as new List<string>();
    
    For Each k As FileInfo In maindir.GetFiles()
        If k.Name.EndsWith(".png") Then
            fileList.Add(k.FullName)
        End If
    Next
    
    'Load Icons to Genre View
    GenreListView1.ItemsSource = fileList
    

    【讨论】:

    • 谢谢,这就像一个魅力,但现在我又面临一个问题,所有的图像都排成一排,虽然有包装面板,但它们没有包装,我应该使用滚动视图,我什至尝试过,但出现了一些错误,说面板只能放在模板中。你能帮我解决这个问题吗?
    • 这个其他答案可以帮助你:stackoverflow.com/questions/1041551/…
    【解决方案2】:

    试试

    <Image Source="{Binding Value}"/>
    

    【讨论】:

    • ArsenMkrt,感谢您的快速回复,但我还没有完成这项工作。
    猜你喜欢
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 2012-10-23
    • 2016-02-13
    相关资源
    最近更新 更多