【问题标题】:Images not getting displayed in Listbox in a user control图像未显示在用户控件的列表框中
【发布时间】:2013-02-28 15:21:18
【问题描述】:

这是我第一次在 WPF 中使用 UserControl。以下是我的 Usercontrol Xaml 和后台 VB 代码:

<UserControl x:Class="AddNewGenre"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" d:DesignHeight="194" d:DesignWidth="405">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="130" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="72*" />
            <ColumnDefinition Width="333*" />
        </Grid.ColumnDefinitions>

        <ListBox Name="GenreListBox" ItemsSource="{Binding}"
                 Grid.Row="1" Grid.Column="1">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel IsItemsHost="True" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Image Source="{Binding Value}" Width="128" Height="128" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>

代码文件:

Public Class AddNewGenre
    Dim Result As HODLib.Classes.GenreClass

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.DataContext = Result
    End Sub

    Private Sub UserControl_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded

        GenreListBox.ItemsSource = GlobalValues.ImageDictionary
    End Sub

End Class

以下是获取 GlobalValues.ImageDictionary 的代码:

Namespace GlobalValues
    Module CurrentVariables
        Public ImageDictionary As Concurrent.ConcurrentDictionary(Of String, BitmapImage)

        Public Sub Initialize()
            ImageDictionary = New Concurrent.ConcurrentDictionary(Of String, BitmapImage)

            'try get resources
            GetAllBitmaps()

        End Sub

        Public Sub GetAllBitmaps()
            Try
                ImageDictionary.Clear()
                With ImageDictionary
                    .TryAdd("Box", New BitmapImage(New Uri("Resources\Box.png", UriKind.Relative)))
                    .TryAdd("Tri", New BitmapImage(New Uri("Resources\Tri.png", UriKind.Relative)))
                End With
            Catch ex As Exception

            End Try
        End Sub

    End Module
End Namespace

问题是图像没有显示在图像列表框中,仅显示黑色项目。你能告诉我我在这里做错了什么吗?

【问题讨论】:

  • 忘了说,图片文件被设置为构建资源类型

标签: wpf vb.net user-controls listbox


【解决方案1】:

从 xaml 的列表框中删除 ItemsSource,因为您已经在代码隐藏文件中设置了它。

<ListBox Name="GenreListBox"
                 Grid.Row="1" Grid.Column="1">

【讨论】:

    【解决方案2】:

    终于从这篇文章中找到了答案:Accessing Build Action Resource type files 在我的例子中,我为格式字符串“/HOD;component/Resources/Box.png”而不是 BitmapImage 赋值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-22
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-20
      相关资源
      最近更新 更多