2012-09-03 67 views
0

這是我第一次在WPF中使用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 

的問題是,圖像沒有顯示在圖像列表框中,只有黑色的項目正在顯示。你能告訴我在這裏做了什麼錯事嗎?

+0

忘了提,圖像文件設置爲構建資源類型 – surpavan

回答

0

從xaml的列表框中刪除ItemsSource,因爲您已將其設置在yur代碼隱藏文件中。

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

我進行了更改,但仍無法正常工作。 – surpavan

相關問題