MVVM模式下WPF動態(tài)綁定展示圖片
MVVM模式下WPF動態(tài)展示圖片,界面選擇圖標,復制到項目中固定目錄下面,保存到數(shù)據(jù)庫的是相對路徑,再次讀取的時候是根據(jù)數(shù)據(jù)庫的相對路徑去獲取項目中絕對路徑的圖片展示。
首先在ViewModel中
//屬性定義
BitmapImage _ImageSource;
/// <summary>
/// 顯示的圖標
/// </summary>
public BitmapImage ImageSource
{
get { return _ImageSource; }
set
{
_ImageSource = value;
NotifyOfPropertyChange("ImageSource");
}
}
string _ImagePath;
/// <summary>
/// 顯示的圖標路徑
/// </summary>
public string ImagePath
{
get { return _ImagePath; }
set
{
_ImagePath = value;
NotifyOfPropertyChange("ImagePath");
}
}
//初始化數(shù)據(jù)
//編輯的時候綁定數(shù)據(jù)
public GroupInfoViewModel(sys_Right_Group groupInfo, OperType type)
{
if (type == OperType.Edit || type == OperType.Show)
{
IsAdd = false;
TitleName = "編輯分組";
RightGroup = groupInfo;
ImagePath = groupInfo.ImagePath;
GetImgData(groupInfo.ImagePath);
}
}
/// <summary>
/// 獲取圖片數(shù)據(jù)
/// </summary>
/// <param name="imgPath">相對路徑</param>
private void GetImgData(string imgPath)
{
if (string.IsNullOrEmpty(imgPath)) return;
try
{
string fileName = System.Environment.CurrentDirectory + imgPath; //獲取文件的絕對路徑
byte[] buf;
if (!PathToByte(fileName, out buf))
{
MessageHelper.ShowAutoCloseWarning("獲取圖標失敗");
return;
}
ImageSource =ByteToImage(buf);
}
catch (Exception ex)
{
throw ex;
}
}
//界面選擇圖片按鈕事件
/// <summary>
/// 修改圖片
/// </summary>
public void ChangedIcon()
{
try
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = string.Format("照片|*.jpg;*.jpeg;*.png;*.gif;*.bmp");
if (open.ShowDialog() == true)
{
var path = open.FileName;
//檢查圖標目錄,絕對路徑下面
string NewPath = System.Environment.CurrentDirectory + @"\Images\Tile\Group\";
string newFile = NewPath + Path.GetFileName(path);
if (!System.IO.Directory.Exists(NewPath))
{
System.IO.Directory.CreateDirectory(NewPath);
}
File.Copy(path, newFile, true); //復制文件到目錄絕對路徑文件夾
FileInfo info = new FileInfo(newFile); //新文件
if (info.Length > MenuViewModel.UserImageMaxLength)
{
MessageHelper.ShowAutoCloseWarning(string.Format("圖標不能大于{0}M",
MenuViewModel.UserImageMaxLength / 1024 / 1024));
return;
}
byte[] buf;
if (!PathToByte(path, out buf))
{
MessageHelper.ShowAutoCloseWarning("修改失敗");
return;
}
ImageSource = ByteToImage(buf);
ImagePath = @"\Images\Tile\Group\" + Path.GetFileName(path); //顯示相對路徑
}
}
catch (Exception ex)
{
throw ex;
}
}
點擊保存的時候再把相對路徑保存到數(shù)據(jù)庫RightGroup.ImagePath = ImagePath;
//公共幫助方法
//把圖片文件轉換為byte數(shù)組
public static bool PathToByte(string path, out byte[] buffer)
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
try
{
buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
return true;
}
catch (Exception ex)
{
buffer = null;
return false;
}
finally
{
if (fs != null)
{
//關閉資源
fs.Close();
}
}
}
//把byte數(shù)組轉化為BitmapImage
public static BitmapImage ByteToImage(byte[] buf)
{
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = new MemoryStream(buf);
bmp.EndInit();
return bmp;
}
View 界面綁定代碼:
<Button Grid.Row="0" Grid.Column="0" Content="選擇圖片" cm:Message.Attach="[Click]=[ChangedIcon()]" Style="{StaticResource BtnOperationStyle}" Height="20" Width="70"></Button>
<Grid Grid.Row="0" Grid.Column="1" Background="LightGray">
<Image Height="120" Width="150" Stretch="Fill" Source="{Binding ImageSource,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></Image>
</Grid>
<Label Grid.Row="1" Grid.Column="0" Style="{StaticResource GridColumnLabelStyle}" Content="路徑:"></Label>
<TextBox Grid.Row="1" Grid.Column="1" Style="{StaticResource StyleForTextBox}" Text="{Binding ImagePath,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Height="30" TextAlignment="Center" IsReadOnly="True"></TextBox>
界面效果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
ASP.NET?MVC5網(wǎng)站開發(fā)用戶注冊(四)
上一次把基本框架搭建起來了,這次開始整Web部分,終于可以看到界面了小激動一下,web項目部分從用戶功能開始,基本有注冊,登錄、注銷、查找、查看、刪除等涉及Member區(qū)域和Manage區(qū)域,供大家參考,具體內容如下2015-09-09
IIS 瀏覽aspx頁面出現(xiàn)無法顯示XML頁的解決方法分享
這篇文章介紹了IIS 瀏覽aspx頁面出現(xiàn)無法顯示XML頁的解決方法,有需要的朋友可以參考一下2013-11-11
MVC+EasyUI+三層新聞網(wǎng)站建立 主頁布局的方法(五)
這篇文章主要為大家詳細介紹了MVC+EasyUI+三層新聞網(wǎng)站建立的第五篇,教大家如何進行主頁布局,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
asp.net core下給網(wǎng)站做安全設置的方法詳解
這篇文章主要給大家介紹了關于asp.net core下給網(wǎng)站做安全設置的相關資料,文章通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-07-07
讓aspx頁面自主控制調用記錄的數(shù)量,類型,隨時更改,不用重新編譯的實現(xiàn)方法
我們經(jīng)常會做一些企業(yè)站點,為了數(shù)據(jù)調用,我們傷透腦筋,我們想方設法讓數(shù)據(jù)顯示變得簡單,又易于維護,這使得我們創(chuàng)造諸于模板之類的東東去搞企業(yè)站,門戶等,2011-08-08
.NET Core利用swagger進行API接口文檔管理的方法詳解
這篇文章主要給大家介紹了關于.NET Core利用swagger進行API接口文檔管理的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-03-03
ASP.NET Core Project.json文件(5)
這篇文章主要為大家詳細介紹了ASP.NET Core Project.json文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
FileUpload使用Javascript檢查擴展名是否有效實現(xiàn)思路
在JavaScript獲取FileUpload控件的文件路徑,并取得路徑中的文件擴展名,再與陣列中的擴展名比較,如果存在,說明上傳的文件是有效的,反之無效,感興趣的朋友可以了解下,或許對你有所幫助2013-02-02
Asp.net 獲取指定目錄下的后綴名為".doc" 的所有文件名和文件路徑
Asp.net 獲取指定目錄下的后綴名為“.doc” 的所有文件名和文件路徑,幫寫一個方法2011-07-07

