注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

回忆之旅

我很向往山村的农庄生活,他们的热情,让我无法抗拒! 51EOO.COM

 
 
 

日志

 
 

silverlight wpf ListBox图片绑定  

2011-12-31 14:39:14|  分类: 技术分享 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
<UserControl
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" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" xmlns:System="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d"
x:Class="SilverlightApplication1.MainPage"
    xmlns:s="clr-namespace:SilverlightApplication1"
Width="640" Height="480">
<UserControl.Resources>
        <s:imageconvert x:Name="imageconvert"></s:imageconvert>
        <s:textconvert x:Name="textconvert"></s:textconvert>
<DataTemplate x:Key="ItemTemplate">
            
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
                <Image Source="{Binding Path=Name}" HorizontalAlignment="Center" Height="100" Width="100"></Image>
</StackPanel>
</DataTemplate>
<Style x:Key="ScrollViewerStyle" TargetType="ScrollViewer">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ScrollViewer">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2">
<Grid Background="{TemplateBinding Background}">
<ScrollContentPresenter x:Name="ScrollContentPresenter" Cursor="{TemplateBinding Cursor}" ContentTemplate="{TemplateBinding ContentTemplate}" Margin="{TemplateBinding Padding}"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ListBoxStyle" TargetType="ListBox">
<Setter Property="Padding" Value="1"/>
<Setter Property="Background" Value="#FFFFFFFF"/>
<Setter Property="Foreground" Value="#FF000000"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="TabNavigation" Value="Once"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="BorderBrush">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFA3AEB9" Offset="0"/>
<GradientStop Color="#FF8399A9" Offset="0.375"/>
<GradientStop Color="#FF718597" Offset="0.375"/>
<GradientStop Color="#FF617584" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<Grid>
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2">
<ScrollViewer x:Name="ScrollViewer" BorderBrush="Transparent" BorderThickness="0" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" TabNavigation="{TemplateBinding TabNavigation}" Style="{StaticResource ScrollViewerStyle}">
<ItemsPresenter />
</ScrollViewer>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
<DataTemplate x:Key="ItemTemplate1">
<StackPanel>
<Image Source="{Binding Property1}" HorizontalAlignment="Left" Height="64" Width="64"/>
<CheckBox IsChecked="{Binding Property2, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid Width="600" Height="400" DataContext="{Binding Source={StaticResource SampleDataSource}}">
<ListBox x:Name="listBox" ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding Collection}" Margin="28,48,36,0" Style="{StaticResource ListBoxStyle}" ItemsPanel="{StaticResource ItemsPanelTemplate1}" Height="104" VerticalAlignment="Top"/>
    </Grid>
</UserControl>

Code:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Effects;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows.Data;

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public BitmapImage GetImage
        {
            get { return new BitmapImage(new Uri("http://www.fdt-7.com/images/index/1_1.jpg", UriKind.RelativeOrAbsolute)); }
        }
        public MainPage()
        {
            // Required to initialize variables
            InitializeComponent();

            List<Student> lstStu = new List<Student>();
            var imageList = new List<string>();
            imageList.Add("http://www.fdt-7.com/images/index/1_1.jpg");
            imageList.Add("http://www.fdt-7.com/images/index/2_3.jpg");
            imageList.Add("http://www.fdt-7.com/images/index/3_2.jpg");
            imageList.Add("http://www.fdt-7.com/images/index/3_4.jpg");
            imageList.Add("http://www.fdt-7.com/images/index/2_1.jpg");
            imageList.Add("http://www.fdt-7.com/images/index/1_4.jpg");
            //ImgList.DataContext = imageList;
            foreach (var item in imageList)
            {
                //Image Image = new System.Windows.Controls.Image();
                //Image.Width = 100;
                //Image.Height = 100;
                //Canvas.SetLeft(Image, left);
                //Canvas.SetTop(Image, 10);
                lstStu.Add(new Student() { Name = new BitmapImage(new Uri(item, UriKind.RelativeOrAbsolute)), Age = 20, Sex = "M" });
                //lstStu.Add(new Student() { Name = item, Age = 20, Sex = "M" });
            }
           
            
            //lstStu.Add(new Student() { Name = "Joey", Age = 21, Sex = "M" });
            //lstStu.Add(new Student() { Name = "Joey", Age = 21, Sex = "M" });
            //lstStu.Add(new Student() { Name = "Jam", Age = 23, Sex = "M" });
            //lstStu.Add(new Student() { Name = "Jone", Age = 24, Sex = "M" });
            //lstStu.Add(new Student() { Name = "Jessi", Age = 25, Sex = "M" });
            //lstStu.Add(new Student() { Name = "Mand", Age = 26, Sex = "M" });

            listBox.ItemsSource = lstStu;
        }


        public class Student
        {
            private ImageSource name;

            public ImageSource Name
            {
                get { return name; }
                set { name = value; }
            }
            private int age;

            public int Age
            {
                get { return age; }
                set { age = value; }
            }
            private string sex;

            public string Sex
            {
                get { return sex; }
                set { sex = value; }
            }
        }


    }

    public class imageconvert : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            /*
             if(value ==null)
return DependencyProperty.UnsetValue;

string url = value.ToString();

if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
{
BitmapImage img = new BitmapImage(new Uri(url, UriKind.RelativeOrAbsolute));
return img;
}
else
return DependencyProperty.UnsetValue;
             */
            if (value == null)
                return DependencyProperty.UnsetValue;
            return new System.Windows.Media.Imaging.BitmapImage(new Uri(value.ToString(), UriKind.RelativeOrAbsolute));
            
        }

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

        #endregion
    }
}

silverlight wpf ListBox图片绑定 - Tony - 回忆之旅

  评论这张
 
阅读(643)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017