Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >silverlight ListBox 多列图片效果

silverlight ListBox 多列图片效果

作者头像
lpxxn
发布于 2018-01-31 04:47:59
发布于 2018-01-31 04:47:59
1.4K00
代码可运行
举报
文章被收录于专栏:技术之路技术之路
运行总次数:0
代码可运行

这个功能之前用wpf写过一次这次用Silverlight写一次

这两种写法上基本上没有太大的差别

这个Demo并不完美,只是给大家提供一个思路

源码:SilverLightListPricture.rar

看一下效果

思路是:

       修改ItemTemplate样式

       ItemsPanelTemplate 用WrapPanel显示

先为image绑定图片添加一个转换类

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SilverLightListPricture
{
    public class ConvertToRecipesImageInfo : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            Stream _stream = value as Stream;
            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(_stream);
            return bitmap;


        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

我先把前端代码分解一下最后给出全部代码 先看一下是怎么修改listbox的ItemTemplate

是用一个image和一个*button做删除

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 <DataTemplate x:Key="ItemTemplate">
            <Grid  Width="200" Height="210" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>

                <Border  BorderThickness="1" BorderBrush="SteelBlue" CornerRadius="3">
                    <Grid   Margin="0">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="185"></RowDefinition>
                            <RowDefinition></RowDefinition>
                        </Grid.RowDefinitions>
                        <Image Grid.Row="0"  Source="{Binding Path=streamsInfo,Converter={StaticResource ImageConverter}}" Margin="0"  ></Image>
                        <StackPanel Grid.Row="1" HorizontalAlignment="Right" >
                            <Button Width="20"  BorderThickness="0" Background="Transparent" Click="Del_PrictureEvent"  Name="btn_Del"  Tag="{Binding Path=activePricture}" Style="{StaticResource CloseButton}" >
                            </Button>
                        </StackPanel>
                    </Grid>
                </Border>
            </Grid>
        </DataTemplate>

button的样式

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<Style x:Key="CloseButton" TargetType="Button">            
            <Setter Property="IsTabStop" Value="False"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Background="Transparent">
                            <Canvas>
                                <Line X1="4" Y1="4" X2="11" Y2="11" Stroke="#9FA1A0" StrokeThickness="2"></Line>
                                <Line X1="11" Y1="4" X2="4" Y2="11" Stroke="#9FA1A0" StrokeThickness="2"></Line>
                            </Canvas>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

listbox用的时候要把它的ItemsPanelTemplate改用WrapPanel

重要的是ScrollViewer.HorizontalScrollBarVisibility是定要为Disabled这样就能防止wrapPanel横向滚动条出现

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 <ListBox Grid.Row="0"   Margin="5" Width="640" Name="lsPricture"  ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                ItemTemplate="{StaticResource ItemTemplate}">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>

                    <tools:WrapPanel Width="Auto" Background="#F3FFFF" >
                    </tools:WrapPanel>

                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>

完整的前台代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<UserControl x:Class="SilverLightListPricture.ListBoxPrictueDEMO"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:convertImage="clr-namespace:SilverLightListPricture"
    xmlns:tools="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit">

    <UserControl.Resources>
        <convertImage:ConvertToRecipesImageInfo x:Key="ImageConverter"/>
        <!--关闭按钮样式-->
        <Style x:Key="CloseButton" TargetType="Button">
            <Setter Property="IsTabStop" Value="False"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Background="Transparent">
                            <Canvas>
                                <Line X1="4" Y1="4" X2="11" Y2="11" Stroke="#9FA1A0" StrokeThickness="2"></Line>
                                <Line X1="11" Y1="4" X2="4" Y2="11" Stroke="#9FA1A0" StrokeThickness="2"></Line>
                            </Canvas>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <DataTemplate x:Key="ItemTemplate">
            <Grid  Width="200" Height="210" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>

                <Border  BorderThickness="1" BorderBrush="SteelBlue" CornerRadius="3">
                    <Grid   Margin="0">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="185"></RowDefinition>
                            <RowDefinition></RowDefinition>
                        </Grid.RowDefinitions>
                        <Image Grid.Row="0"  Source="{Binding Path=streamsInfo,Converter={StaticResource ImageConverter}}" Margin="0"  ></Image>
                        <StackPanel Grid.Row="1" HorizontalAlignment="Right" >
                            <Button Width="20"  BorderThickness="0" Background="Transparent" Click="Del_PrictureEvent"  Name="btn_Del"  Tag="{Binding Path=activePricture}" Style="{StaticResource CloseButton}" >
                            </Button>
                        </StackPanel>
                    </Grid>
                </Border>
            </Grid>
        </DataTemplate>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="600"></RowDefinition>
            <RowDefinition Height="73"></RowDefinition>
        </Grid.RowDefinitions>
        <ListBox Grid.Row="0"   Margin="5" Width="640" Name="lsPricture"  ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                ItemTemplate="{StaticResource ItemTemplate}">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>

                    <tools:WrapPanel Width="Auto" Background="#F3FFFF" >
                    </tools:WrapPanel>

                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>

        <StackPanel Grid.Row="1"  VerticalAlignment="Center" HorizontalAlignment="Center"  Orientation="Horizontal">
            <Button Content="添加 " Width="120" Click="btn_AddEvent"></Button>
        </StackPanel>
    </Grid>
</UserControl>

后台代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverLightListPricture
{
    public partial class ListBoxPrictueDEMO : UserControl
    {
        ObservableCollection<ImageInfo> SourceCollection = new ObservableCollection<ImageInfo>();
        public ListBoxPrictueDEMO()
        {
            InitializeComponent();
            bindSource();
        }
        //删除
        public void Del_PrictureEvent(object sender, RoutedEventArgs e)
        {
           
        }
        void bindSource()
        {
            lsPricture.ItemsSource = SourceCollection;
        }

        public void btn_AddEvent(object sender, RoutedEventArgs e)
        {

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "图片文件(*.jpg,*.png,*.bmp)|*.jpg;*.png;*.bmp|All Files (*.*)|*.*";
            if (openFileDialog.ShowDialog() == true)
            {
                FileInfo file = openFileDialog.File;
                
                Stream stream = file.OpenRead();
                
                SourceCollection.Add(new ImageInfo { streamsInfo = stream, activePricture = "tag" });
                
            }
        }
    }

    public class ImageInfo
    {
        public string activePricture
        {
            get;
            set;
        }
        public Stream streamsInfo
        {
            get;
            set;
        }
    }
}

 好了就说到这

源码:SilverLightListPricture.rar

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013-11-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
WPF实现消息中心
本文将讲解基于WPF实现一个消息中心的功能,比如常见的软件当中会经常收到服务端推送的“新闻”、“公告”等消息。这个时候就需要对这个需求进行分析了。
JusterZhu
2022/12/07
6100
WPF实现消息中心
wpf listBox 多列大图片效果
修改ListBox的模版 多列大图片效果,加上删除button 看图 上代码! <Window x:Class="Thunder.SetCenter.RoomSetting.ActivityPhoto
lpxxn
2018/01/31
2.4K0
wpf  listBox  多列大图片效果
WPF实现列表分页控件的示例代码分享
[TemplatePart(Name = CountPerPageTextBoxTemplateName, Type = typeof(TextBox))]
用户7718188
2022/11/06
1.3K0
SilverLight企业应用框架设计【二】框架画面
注意,这里每个顶部菜单的ICO图标不是动态的,朋友们,想让他变成动态的就自己动手吧
liulun
2022/05/09
6120
SilverLight企业应用框架设计【二】框架画面
张高兴的 UWP 开发笔记:横向 ListView
  ListView 默认的排列方向是纵向 ( Orientation="Vertical" ) ,但如果我们需要横向显示的 ListView 怎么办? Blend for Visual Studio 现在就派上用场了。不只是 ListView ,其他的控件也可以用 Blend 定制你自己的 UI 样式。   下面新建一个项目 "HorizontalListViewDemo" ,用于演示横向 ListView ,解决方案结构如下:( GitHub: https://github.com/ZhangGaox
张高兴
2018/05/18
1.2K0
win10 uwp 商业游戏 界面添加图标感谢
在显示启动的时候,是需要加载游戏需要使用的资源,如果觉得这时需要控制进度条,就需要使用注入的方法,给他知道现在的进度,不过我现在不去做这里,于是就很简单的代码做出来启动页面。
林德熙
2018/09/18
7940
win10 uwp 商业游戏 
            界面添加图标感谢
Silverlight之ListBox/Style学习笔记--ListBox版的图片轮换广告
ListBox是一个很有用的控件,其功能直逼Asp.Net中的Repeater,它能实现自定义数据项模板,纵向/横向排列Item(如果扩展一下实现自行折行,几乎就是SL版的Repeater了--实际上WrapPanel已经实现了,不过没有默认集成在SL3中).  这里推荐一个老外的文章 http://blogs.msdn.com/delay/archive/2008/03/05/lb-sv-faq-examples-notes-tips-and-more-for-silverlight-2-beta-1-s
菩提树下的杨过
2018/01/22
1K0
Silverlight之ListBox/Style学习笔记--ListBox版的图片轮换广告
dotnet Microsoft.Recognizers.Text 超强大的自然语言关键词提取库
本文和大家介绍一个使用超级简单,但是功能特别强大的自然语言关键词提取库,可以根据输入的自然语言提取出里面的信息。例如我在一句话里面说了哪些数值变量或者说了手机号码等
林德熙
2020/08/04
9780
dotnet Microsoft.Recognizers.Text 超强大的自然语言关键词提取库
Silvelright:ListBox无法用Tab顺序切换内部元素焦点的解决
默认情况下,Silverlight自带的ListBox控件如果内部有多个TextBox,用户无法用键盘上的Tab键,在ListBox内部的TextBox之间切换。但Teterik RadControls 中的telerik:ListBox却很好的解决了这个问题,只要把telerik:ListBox的IsTabStop设置成false,同时把TabNavigation设置成Local即可(而SL自带的ListBox就算设置了这二个属性,Tab键需要按二次才能切换焦点) 完整Xaml代码: <UserContr
菩提树下的杨过
2018/01/24
1.2K0
【愚公系列】2023年04月 WPF运动控制平台-003.运动控制平台的UI设计
---- 一、运动控制平台的UI设计 1.代码 <Window x:Class="MotionPlatform.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expr
愚公搬代码
2023/04/16
4900
【愚公系列】2023年04月 WPF运动控制平台-003.运动控制平台的UI设计
listbox里面添加WrapPanel ,支持自适应换行[通俗易懂]
listbox大家都会用,如果要让它支持换行操作还必须加上 ListBox.ItemsPanel ItemsPanelTemplate toolkit:WrapPanel/ /ItemsPanelTemplate /ListBox.ItemsPanel 但是也有问题了,必须设置WrapPanel的宽度,也就是不能自适应宽度去调整每一行的宽度,这样的后果可能会出现要么全部推在一起,要么要有横向的滚动条
全栈程序员站长
2022/09/15
1.3K0
【愚公系列】2023年04月 WPF运动控制平台-005.运动平台之功能实现(完结)
---- 一、运动平台之功能实现 1.位置计算 物理可用距离 (40000), 取料位:19945P, 打包位:-19360P 像素位置:取料位:20px 打包位:1020px 把脉冲转换位距离 步进驱动器设置细分 8 步进电机步进角 1.8° 导程:8mm 计算步进电机走1cm需要的脉冲数 360 / 1.8 = 200个脉冲转一圈 200 * 8 = 1600个脉冲转一圈(细分情况) 1600 / 8 = 200 (步进电机走1mm需要) 故:走1cm需要脉冲
愚公搬代码
2023/04/28
4500
【愚公系列】2023年04月 WPF运动控制平台-005.运动平台之功能实现(完结)
[WPF自定义控件库]为Form和自定义Window添加FunctionBar
我常常看到同一个应用程序中的表单的按钮————也就是“确定”、“取消”那两个按钮————实现得千奇百怪,其实只要使用统一的Style起码就可以统一按钮的大小,而我喜欢更进一步将”确定“、”取消“或其它按钮封装进一个自定义控件里。
dino.c
2019/06/11
7810
[WPF自定义控件库]为Form和自定义Window添加FunctionBar
[Silverlight]用ListBox实现SlideShow
用Silverlight2整整一年了,上个星期公司全面转去Silverlight3,作为纪念就把用SL2写的最后一个东西发出来吧。效果如下:
dino.c
2019/01/18
5980
[Silverlight]用ListBox实现SlideShow
WPF中ListBox的WrapPanel布局「建议收藏」
======================================================
全栈程序员站长
2022/09/15
6800
WPF中ListBox的WrapPanel布局「建议收藏」
如何实现日期范围选择器
原文链接: https://github.com/WPFDevelopersOrg/WPFDevelopers
郑子铭
2024/12/20
1120
如何实现日期范围选择器
《深入浅出WPF》——模板学习
图形用户界面(GUI,Graphic User Interface)应用较之控制台界面(CUI,Command User Interface)应用程序最大的好处就是界面友好、数据显示直观。CUI程序中数据只能以文本的形式线性显示,GUI程序则允许数据以文本、列表、图形等多种形式立体显示。 用户体验在GUI程序设计中起着举足轻重的作用——用户界面设计成什么样子看上去才够漂亮?控件如何安排才简单易用并且少犯错误?(控件并不是越复杂越好)这些都是设计师需要考虑的问题。WPF系统不但支持传统Windows Forms(简称WinForm)编程的用户界面和用户体验设计,更支持使用专门的设计工具Microsoft Expression Blend进行专业设计,同时还推出了以模板为核心的新一代设计理念(这是2010年左右的书,在那时是新理念,放现在较传统.NET开发也还行,不属于落后的技术)。 本章我们就一同来领略WPF强大的模板功能的风采。
全栈程序员站长
2022/09/09
5K0
《深入浅出WPF》——模板学习
扩展GridView控件——为内容项添加拖放及分组功能
引言 相信大家对GridView都不陌生,是非常有用的控件,用于平铺有序的显示多个内容项。打开任何WinRT应用或者是微软合作商的网站,都会在APP中发现GridView的使用。“Tiles”提供了一个简单易用,平铺方式来组织内容显示。Windows8的开始菜单是最典型的GridView 示例。“开始菜单”显示了系统中安装的所有应用程序,而且支持重新排列。 本文源于我们项目的开发人员,他们想在项目中提供与GridView相同的用户体验,想要创建类GridView控件。 GridView 可以显示大小不定的内
葡萄城控件
2018/01/10
3K0
扩展GridView控件——为内容项添加拖放及分组功能
【愚公系列】2022年10月 基于WPF的智能制造MES系统框架-菜单栏的设计
MES系统为企业提供包括制造数据管理、计划排程管理、生产调度管理、库存管理、质量管理、人力资源管理、工作中心/设备管理、工具工装管理、采购管理、成本管理、项目看板管理、生产过程控制、底层数据集成分析、上层数据集成分解等管理模块,为企业打造一个扎实、可靠、全面、可行的制造协同管理平台。
愚公搬代码
2022/10/28
7380
请来围观:WPF开发的微信客户端!!!
公司的同事离职了,接下来的日子可能会忙碌,能完善DEMO的时间也会少了,因此,把做的简易DEMO整体先记录一下,等后续不断的完善。
沙漠尽头的狼
2022/06/13
1.9K0
请来围观:WPF开发的微信客户端!!!
推荐阅读
相关推荐
WPF实现消息中心
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文