WPF 照片墙的实现
主要参照了DevExpress的PhotoGallery实例的实现。
效果如下:

照片墙核心代码如下:
PhotoGallery.xaml
<local:CarouselDemoModule x:Class="PictureMagic.PhotoGallery"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxca="http://schemas.devexpress.com/winfx/2008/xaml/carousel"
xmlns:dxdb="http://schemas.devexpress.com/winfx/2008/xaml/demobase"
xmlns:local="clr-namespace:PictureMagic"
xmlns:collection="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" >
<dxdb:DemoModuleControl>
<Grid ClipToBounds="False" Background="#FFB6C1">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition Height="" />
</Grid.RowDefinitions>
<dxca:CarouselPanel RenderOptions.BitmapScalingMode="HighQuality"
x:Name="carousel"
AnimationTime=""
VisibleItemCount=""
AttractorPointIndex=""
PathSizingMode="Stretch"
PathPadding="10,5,10,20"
IsAutoSizeItem="True"
ClipToBounds="True"
PathVisible="False"
IsInvertedDirection="True"
Grid.RowSpan=""
IsRepeat="True"
ItemSize="100,100" >
<dxca:CarouselPanel.Resources>
<ControlTemplate x:Key="itemTemplate" TargetType="{x:Type ContentControl}">
<Grid VerticalAlignment="Center">
<Border Margin="3,3,0,0" Background="Black" Opacity="0.25" CornerRadius="" />
<Border Margin="0,0,3,3" Padding="" BorderBrush="#5F000000" BorderThickness="" Background="White">
<Image Source="{Binding Path=DataContext, RelativeSource={RelativeSource TemplatedParent}}" Stretch="Uniform" />
</Border>
</Grid>
</ControlTemplate>
<Style TargetType="{x:Type FrameworkElement}" x:Key="itemStyle">
<Setter Property="RenderTransformOrigin" Value="0.5, 0.5" />
<Setter Property="Opacity" Value="{Binding Path=(dxca:CarouselPanel.Parameters).Opacity, RelativeSource={RelativeSource Self}}" />
<Setter Property="Panel.ZIndex" Value="{Binding Path=(dxca:CarouselPanel.Parameters).ZIndex, Converter={local:DoubleToIntConverter}, RelativeSource={RelativeSource Self}}" />
<Setter Property="RenderTransform">
<Setter.Value>
<TransformGroup>
<ScaleTransform ScaleX="{Binding Path=(dxca:CarouselPanel.Parameters).Scale, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContentControl}}}"
ScaleY="{Binding Path=(dxca:CarouselPanel.Parameters).Scale, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContentControl}}}"
/>
<TranslateTransform X="{Binding Path=(dxca:CarouselPanel.Parameters).OffsetX, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContentControl}}}"
Y="{Binding Path=(dxca:CarouselPanel.Parameters).OffsetY, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContentControl}}}"
/>
</TransformGroup>
</Setter.Value>
</Setter>
</Style>
</dxca:CarouselPanel.Resources>
<dxca:CarouselPanel.ParameterSet>
<dxca:ParameterCollection>
<dxca:Parameter Name="Opacity" DistributionFunction="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type dxca:CarouselPanel}, ResourceId=Linear3PConvexNr}}" />
<dxca:Parameter Name="Scale" DistributionFunction="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type dxca:CarouselPanel}, ResourceId=Linear3PConvexNr}}" />
<dxca:Parameter Name="ZIndex" DistributionFunction="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type dxca:CarouselPanel}, ResourceId=Linear3PConvexERIntMax}}" />
</dxca:ParameterCollection>
</dxca:CarouselPanel.ParameterSet>
<dxca:CarouselPanel.ItemMovingPath>
<PathGeometry Figures="M255,0 C352.86864,0.5 454.5,61.389274 454.5,136.5 C454.5,211.61073 352.86864,272.5 227.5,272.5 C102.13136,272.5 0.5,211.61073 0.5,136.5 C0.5,61.389274 102.13136,0.5 200,0.5 " />
</dxca:CarouselPanel.ItemMovingPath>
</dxca:CarouselPanel>
<dxdb:ImageControl Margin="30,30,30,0" Source="{Binding ElementName=carousel, Path=ActiveItem.DataContext}" VerticalAlignment="Center" HorizontalAlignment="Center" />
<dxca:CarouselNavigator
VerticalAlignment="Center"
HorizontalAlignment="Center"
MinHeight=""
MinWidth=""
Grid.Row=""
Carousel="{Binding ElementName=carousel}"
/>
</Grid>
</dxdb:DemoModuleControl>
</local:CarouselDemoModule>
PhotoGallery.xaml.cs
using System.Windows;
using System.Windows.Controls;
using DevExpress.Xpf.Carousel;
using System.Collections.Generic; namespace PictureMagic
{
public partial class PhotoGallery : CarouselDemoModule { private List<PicutureInfo> m_picureInfoList = null;
public PhotoGallery() {
InitializeComponent();
m_picureInfoList = new List<PicutureInfo>();
}
protected override void AddItems(string path, ItemType it, CarouselPanel carousel) {
var items = CreateItems(path, it);
foreach (var item in items) {
ContentControl control = new ContentControl();
control.Template = carousel.Resources["itemTemplate"] as ControlTemplate;
control.Style = carousel.Resources["itemStyle"] as Style;
control.DataContext = ((Image)item).Source;
carousel.Children.Add(control);
}
} public void ShowNextImage()
{
carousel.MoveNext();
} public void SearchImage(string path)
{
carousel.Children.Clear();
AddItems(path, ItemType.BinaryImage, carousel);
}
}
}
CarouselDemoModule.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using DevExpress.Utils;
using DevExpress.Xpf.Carousel;
using DevExpress.Xpf.DemoBase; namespace PictureMagic
{
public class CarouselDemoModule : DemoModule
{
static CarouselDemoModule()
{
Type ownerType = typeof(CarouselDemoModule);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
CommandManager.InvalidateRequerySuggested();
}
protected virtual List<FrameworkElement> CreateItems(string path, ItemType it)
{
ContentLoadHelper contentLoadHelper = new ContentLoadHelper();
contentLoadHelper.Path = path;
var itemList = new List<FrameworkElement>(contentLoadHelper.LoadItems(it).ToArray());
for (int i = ; i < itemList.Count; i++)
{
itemList[i].Name = "Item" + i.ToString();
((Image)itemList[i]).Stretch = System.Windows.Media.Stretch.Fill;
}
return itemList;
}
protected virtual void AddItems(string path, ItemType it, CarouselPanel carousel)
{
var itemList = CreateItems(path, it);
foreach (var item in itemList)
{
item.Name = "item" + carousel.Children.Count;
carousel.Children.Add(item);
}
}
}
}
Utils.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Data;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging; namespace PictureMagic
{
public enum ItemType { BinaryImage, DrawingImage }
public class ContentLoadHelper
{
public string Path { get; set; }
public ContentLoadHelper()
{
}
public List<FrameworkElement> LoadItems(ItemType it)
{
LoadDelegate loadDelegate = null;
switch (it)
{
case ItemType.BinaryImage:
loadDelegate = LoadImage;
break;
case ItemType.DrawingImage:
// loadDelegate = LoadDrawingImage;
break;
}
var items = new List<FrameworkElement>();
if (loadDelegate != null)
{
DirectoryInfo folder = new DirectoryInfo(Path); foreach (FileInfo file in folder.GetFiles("*.*"))
{
if (IsPhotoFile(file.Extension))
{
items.Add(loadDelegate(file.FullName));
}
}
}
return items;
} private bool IsPhotoFile(string extension)
{
string[] exArray = { ".PNG", ".JPG", ".BMP", ".GIF", ".JPEG" };
foreach (string strExe in exArray)
{
if (extension.ToUpper().Equals(strExe))
{
return true;
}
}
return false;
} public delegate FrameworkElement LoadDelegate(string strPath);
public Image LoadDrawingImage(Stream stream)
{
var rd = (ResourceDictionary)XamlReader.Load(stream);
var di = (DrawingImage)rd["Layer_1"];
return new Image() { Source = di };
}
public Image LoadImage(string strPath)
{
var image = new Image();
image.Source = new BitmapImage(new Uri(strPath));
return image;
}
} public class DoubleToIntConverter : MarkupExtension, IValueConverter
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (targetType != typeof(int))
throw new InvalidOperationException();
return (int)((double)value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
}
WPF 照片墙的实现的更多相关文章
- 牛逼哄哄的Qt库
目录 一.有价值 - 好的网站 - 好的文章 二.Qt开源库-工具 - QtXlsx--excel读写库 三.Qt开源库-控件 - libqxt编译 - Qwt - QCustomPlot - 其他 ...
- WPF 制作聊天窗口获取历史聊天记录
腾讯从QQ2013版起开始在聊天记录里添加了历史记录查看功能,个人聊天窗口可以点击最上边的‘查看历史消息’,而群组里的未读消息可以通过滚动鼠标中键或者拖动滚动条加载更多消息,那这个用wpf怎么实现呢? ...
- wpf 自定义窗口,最大化时不覆盖任务栏
相信很多人使用wpf时会选择自定义美观的窗口,因此会设置WindowStyle="None" 取消自带的标题栏.但这样使用 WindowState="Maximized& ...
- 在WPF中使用依赖注入的方式创建视图
在WPF中使用依赖注入的方式创建视图 0x00 问题的产生 互联网时代桌面开发真是越来越少了,很多应用都转到了浏览器端和移动智能终端,相应的软件开发上的新技术应用到桌面开发的文章也很少.我之前主要做W ...
- MVVM框架从WPF移植到UWP遇到的问题和解决方法
MVVM框架从WPF移植到UWP遇到的问题和解决方法 0x00 起因 这几天开始学习UWP了,之前有WPF经验,所以总体感觉还可以,看了一些基础概念和主题,写了几个测试程序,突然想起来了前一段时间在W ...
- MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息
MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二 ...
- MVVM模式解析和在WPF中的实现(五)View和ViewModel的通信
MVVM模式解析和在WPF中的实现(五) View和ViewModel的通信 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 M ...
- MVVM设计模式和WPF中的实现(四)事件绑定
MVVM设计模式和在WPF中的实现(四) 事件绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...
- MVVM模式解析和在WPF中的实现(三)命令绑定
MVVM模式解析和在WPF中的实现(三) 命令绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...
随机推荐
- 从arduino到32单片机的转型
#include "stm32f10x.h" #include "led.h" #include "delay.h" int main(vo ...
- mybatis分页+springmvc+jsp+maven使用步骤
作者注:本文主要用于个人学习.复习.同时欢迎指导讨论 1,添加maven依赖<dependency> <groupId>com.github.miemiedev ...
- 转-CSS padding margin border属性详解
原文链接:http://www.cnblogs.com/linjiqin/p/3556497.html 图解CSS padding.margin.border属性W3C组织建议把所有网页上的对像都放在 ...
- Servlet--j2e中文乱码解决
我们在写项目的时候经常会传递一些中文参数,但是j2e默认使用ISO-8859-1来编码和解码,所以很容易出现中文乱码问题.这里我做一个统一的整理,其实这里的中文乱码问题和上一篇的路径问题都是j2e经常 ...
- JDBC学习笔记(四)
减少各个Dao类间的重复代码,有以下几种方式: 写一个DBConnectionManager,将公共的查询逻辑做成方法,将sql语句作为参数传递给方法. public class DBConnecti ...
- BZOJ 2959: 长跑 [lct 双连通分量 并查集]
2959: 长跑 题意:字词加入边,修改点权,询问两点间走一条路径的最大点权和.不一定是树 不是树
- BZOJ 2734: [HNOI2012]集合选数 [DP 状压 转化]
传送门 题意:对于任意一个正整数 n≤100000,如何求出{1, 2,..., n} 的满足若 x 在该子集中,则 2x 和 3x 不能在该子集中的子集的个数(只需输出对 1,000,000,001 ...
- BZOJ 3238: [Ahoi2013]差异 [后缀自动机]
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2512 Solved: 1140[Submit][Status ...
- Xposed 初尝
1.初次使用xposed,之前直至其名,不闻其用. 2.相关引用配置上图 build.gradle配置,如下: dependencies { compile fileTree(include: ['* ...
- ettercap+urlsnarf+driftnet+wireshark监听妹子上网
搞事肯定得确认目标.所以我们得先确认一个目标 确认目标这种事情不多说. 1.开启IP转发 echo 1 > /proc/sys/net/ipv4/ip_forward 然后ettercap ...