河南省建设协会网站微信网站搭建教程
河南省建设协会网站,微信网站搭建教程,重庆市建设工程信息网安全监督下载中心,长链接转换成短链接工具前言 最近在和其他软件联合做一个本地图片选择传输功能#xff0c;为此希望图片能够有序的呈现在客户端#xff0c;简单的实现了一下功能#xff0c;通过Mvvm模式进行呈现#xff0c;过程简单通俗#xff0c;话不多说直接上图。 处理过程 前台代码
你只需要粘贴到你的前台…前言 最近在和其他软件联合做一个本地图片选择传输功能为此希望图片能够有序的呈现在客户端简单的实现了一下功能通过Mvvm模式进行呈现过程简单通俗话不多说直接上图。 处理过程 前台代码
你只需要粘贴到你的前台xml中就可以位置记得调整下Margin,我这是按照我的位置进行调整的所以针对ListBox在你的前台你还需要调整下。 ListBox NamelstFileManager Background Transparent ItemsSource{Binding} ScrollViewer.HorizontalScrollBarVisibilityDisabled ScrollViewer.VerticalScrollBarVisibilityAuto ScrollViewer.CanContentScrollTrue Margin69,192,50,40ListBox.ItemsPanelItemsPanelTemplateWrapPanel//ItemsPanelTemplate/ListBox.ItemsPanelListBox.ItemTemplateDataTemplate!--这里修改内容整体大小以及在你框内的占比我这一行显示5个--Grid Margin17 Width100 Height155Grid.RowDefinitionsRowDefinition HeightAuto /RowDefinitionRowDefinition HeightAuto /RowDefinitionRowDefinition HeightAuto /RowDefinition/Grid.RowDefinitionsImage Source{Binding Pic} HorizontalAlignmentCenter VerticalAlignmentCenter Width100 Height100/Border BorderThickness1 BorderBrushred Margin1,107,1,0/TextBlock Text{Binding Name} Grid.Row1 ForegroundWhite VerticalAlignmentCenter HorizontalAlignmentCenter HeightAuto TextWrappingWrap//Grid/DataTemplate/ListBox.ItemTemplate
/ListBox
后台代码
创建一个类进行数据绑定 public class LVData{public string Name { get; set; }public BitmapImage Pic { get; set; }} 定义一个集合进行数据缓存 集合定义在MainWindow的类中 ObservableCollectionLVData LVDatas new ObservableCollectionLVData(); 在我们的逻辑中进行数据填充和呈现清除集合清空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 的交互逻辑/// /summarypublic partial class MainWindow : Window{//定义集合ObservableCollectionLVData LVDatas new ObservableCollectionLVData();public MainWindow(){InitializeComponent();ImageTexture2DView(E:\\ProjectFiles\\ImageTexture);}private void ImageTexture2DView(string path){//Path是图片所在的文件夹路径var apps System.IO.Directory.GetFiles(path);Liststring images new Liststring();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; }}
}结局 后续想从数据库或者其他地方添加就根据自己的想法添加就可以了另外获取点击的是哪个绑定个监听事件就可以了希望对大家有帮助。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/88841.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!