我是wpf的新手。我创建了listview以统一显示图像。我做了一个转换器类,将byte []转换为位图图像。在这里,图像是从数据库动态加载的。加载listview时,不显示任何内容。我已附上我的源代码。
这是我的xaml代码:
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding price}" />
<Image Source="{Binding Path=ImageFile,RelativeSource={RelativeSource AncestorType=ListView}
,Converter={StaticResource ImageConverter}}" Height="300" Width="300" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="5" HorizontalAlignment="Stretch"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListView>
这是我的转换器类代码:
public sealed class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
return new BitmapImage(new Uri((string)value));
}
catch
{
return new BitmapImage();
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
这是列表框绑定代码:
private void clickme_Click(object sender, RoutedEventArgs e)
{
string connection = ConfigurationManager.ConnectionStrings["POSconnection"].ConnectionString;
SqlConnection con = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand("SP_Getsubitem", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@getname","Pasta");
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
DataSet ds = new DataSet();
da.Fill(dt);
getdatas.ItemsSource = dt.DefaultView;
}