C#?WPF?ListBox?動態(tài)顯示圖片功能
前言
最近在和其他軟件聯(lián)合做一個本地圖片選擇傳輸功能,為此希望圖片能夠有序的呈現在客戶端,簡單的實現了一下功能,通過Mvvm模式進行呈現,過程簡單通俗,話不多說直接上圖。

處理過程
前臺代碼
你只需要粘貼到你的前臺xml中就可以,位置記得調整下Margin,我這是按照我的位置進行調整的,所以針對ListBox在你的前臺你還需要調整下。
<ListBox Name="lstFileManager" Background ="Transparent" ItemsSource="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="True" Margin="69,192,50,40">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<!--這里修改內容整體大小以及在你框內的占比,我這一行顯示5個-->
<Grid Margin="17" Width="100" Height="155">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" ></RowDefinition>
<RowDefinition Height="Auto" ></RowDefinition>
<RowDefinition Height="Auto" ></RowDefinition>
</Grid.RowDefinitions>
<Image Source="{Binding Pic}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="100"/>
<Border BorderThickness="1" BorderBrush="red" Margin="1,107,1,0"/>
<TextBlock Text="{Binding Name}" Grid.Row="1" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" Height="Auto" TextWrapping="Wrap"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>后臺代碼
創(chuàng)建一個類進行數據綁定
public class LVData
{
public string Name { get; set; }
public BitmapImage Pic { get; set; }
}定義一個集合進行數據緩存 (集合定義在MainWindow的類中)
ObservableCollection<LVData> LVDatas = new ObservableCollection<LVData>();
在我們的邏輯中進行數據填充和呈現,清除集合清空ListBox中的Item顯示
//添加圖
LVDatas.Add(new LVData { Name = "圖片在ListBox中顯示的名稱(建議直接顯示圖片名稱)", Pic = new BitmapImage(new Uri("完整的圖片路徑")) });
//顯示在ListBox中
lstFileManager.ItemsSource = LVDatas;
//清除集合清空呈現
LVDatas.Clear();
//當前點擊的圖片名稱(lstFileManager.SelectedIndex 這是目前點擊的下標)
Console.WriteLine(LVDatas[lstFileManager.SelectedIndex].Name);整體代碼
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ImageTexture
{
/// <summary>
/// MainWindow.xaml 的交互邏輯
/// </summary>
public partial class MainWindow : Window
{
//定義集合
ObservableCollection<LVData> LVDatas = new ObservableCollection<LVData>();
public MainWindow()
{
InitializeComponent();
ImageTexture2DView("E:\\ProjectFiles\\ImageTexture");
}
private void ImageTexture2DView(string path)
{
//Path是圖片所在的文件夾路徑
var apps = System.IO.Directory.GetFiles(path);
List<string> images = new List<string>();
foreach (string app in apps)//---遍歷文件夾所有文件
{
var fi = new FileInfo(app);//---使用FileInfo類進行操作
if (fi.Extension == ".png")
{
//將圖片添加到LVData中
LVDatas.Add(new LVData { Name = fi.Name.Remove(fi.Name.LastIndexOf(".")), Pic = new BitmapImage(new Uri(fi.FullName)) });
}
}
//進行呈現
lstFileManager.ItemsSource = LVDatas;
}
private void ImageClear_Click(object sender, RoutedEventArgs e)
{
//清除集合清空ListBox中的Item顯示
LVDatas.Clear();
}
}
public class LVData
{
public string Name { get; set; }
public BitmapImage Pic { get; set; }
}
}結局
后續(xù)想從數據庫或者其他地方添加就根據自己的想法添加就可以了,另外獲取點擊的是哪個綁定個監(jiān)聽事件就可以了,希望對大家有幫助。
到此這篇關于C# WPF ListBox 動態(tài)顯示圖片的文章就介紹到這了,更多相關C# WPF ListBox 顯示圖片內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
DevExpress設置TreeList圖片節(jié)點背景色的方法
這篇文章主要介紹了DevExpress設置TreeList圖片節(jié)點背景色的方法,需要的朋友可以參考下2014-08-08
C# Lambda表達式select()和where()的區(qū)別及用法
這篇文章主要介紹了C# Lambda表達式select()和where()的區(qū)別及用法,select在linq中一般會用來提取最后篩選的元素集合,在lambda表達式中通常用where得到元素集合,需要的朋友可以參考下2023-07-07
python實現AutoResetEvent類的阻塞模式方法解析
AutoResetEvent :當某個線程執(zhí)行到WaitOne()方法時,該線程則會處于阻塞模式,當被調用了Set()方法,阻塞的線程則會繼續(xù)向下執(zhí)行,其狀態(tài)立即被自動設置為阻塞模式2012-11-11

