4 个解决方案
#1
就是个自定义列表,然后添加datasources属性.我自己做了个自定义控件,如图.数据是image和序号和名称.
picturebox里面放.Image. Lable就是序号和名称.现在不知道要怎么样把这组合个控件和datasource绑定,能教教我吗?
#2
using System;
using System.Linq;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
string[] animals = new string[]
{
"alligator", "ants", "bear", "bee", "cat", "snail", "snake",
"cheetah", "cow", "deer", "dolphin", "dragonfly", "spider", "squirrel",
"duck", "elephant", "frog", "giraffe", "ladybug", "starfish",
};
this.DataContext = animals.Select(x => new My(x)).ToList();
}
}
class My
{
public My(string name)
{
this.Name = name;
this.ImageSource = string.Format("http://www.sciencekids.co.nz/images/pictures/animals/{0}.jpg", name);
}
public string Name { get; set; }
public string ImageSource { get; set; }
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Window.Resources>
<DrawingBrush x:Key="circleMask">
<DrawingBrush.Drawing>
<GeometryDrawing Brush="White" Geometry="M -1,0 A 1,1 0 1 1 1,0 M -1,0 A 1,1 0 1 0 1,0" />
</DrawingBrush.Drawing>
</DrawingBrush>
<DataTemplate x:Key="my">
<StackPanel Orientation="Vertical" Margin="0 10 0 10">
<Image Source="{Binding ImageSource}" Width="200" Height="200" Stretch="Fill" OpacityMask="{StaticResource circleMask}" />
<TextBlock Text="{Binding Name}" FontSize="16" HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>
<ItemsPanelTemplate x:Key="threeRowPanel">
<UniformGrid Columns="3" />
</ItemsPanelTemplate>
</Window.Resources>
<DockPanel>
<ContentControl ContentTemplate="{StaticResource my}" Content="{Binding ElementName=listbox, Path=SelectedItem}" Height="240" DockPanel.Dock="Bottom" />
<ListBox Name="listbox" ItemTemplate="{StaticResource my}" ItemsSource="{Binding}" ItemsPanel="{StaticResource threeRowPanel}" DockPanel.Dock="Top" />
</DockPanel>
</Window>
#3
Mark yi xia
#4
一下子加载1000多涨图片到内存,是不是有点浪费资源啊。
单击弹出一个窗体就能解决你的第二个问题
单击弹出一个窗体就能解决你的第二个问题
#1
就是个自定义列表,然后添加datasources属性.我自己做了个自定义控件,如图.数据是image和序号和名称.
picturebox里面放.Image. Lable就是序号和名称.现在不知道要怎么样把这组合个控件和datasource绑定,能教教我吗?
#2
using System;
using System.Linq;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
string[] animals = new string[]
{
"alligator", "ants", "bear", "bee", "cat", "snail", "snake",
"cheetah", "cow", "deer", "dolphin", "dragonfly", "spider", "squirrel",
"duck", "elephant", "frog", "giraffe", "ladybug", "starfish",
};
this.DataContext = animals.Select(x => new My(x)).ToList();
}
}
class My
{
public My(string name)
{
this.Name = name;
this.ImageSource = string.Format("http://www.sciencekids.co.nz/images/pictures/animals/{0}.jpg", name);
}
public string Name { get; set; }
public string ImageSource { get; set; }
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Window.Resources>
<DrawingBrush x:Key="circleMask">
<DrawingBrush.Drawing>
<GeometryDrawing Brush="White" Geometry="M -1,0 A 1,1 0 1 1 1,0 M -1,0 A 1,1 0 1 0 1,0" />
</DrawingBrush.Drawing>
</DrawingBrush>
<DataTemplate x:Key="my">
<StackPanel Orientation="Vertical" Margin="0 10 0 10">
<Image Source="{Binding ImageSource}" Width="200" Height="200" Stretch="Fill" OpacityMask="{StaticResource circleMask}" />
<TextBlock Text="{Binding Name}" FontSize="16" HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>
<ItemsPanelTemplate x:Key="threeRowPanel">
<UniformGrid Columns="3" />
</ItemsPanelTemplate>
</Window.Resources>
<DockPanel>
<ContentControl ContentTemplate="{StaticResource my}" Content="{Binding ElementName=listbox, Path=SelectedItem}" Height="240" DockPanel.Dock="Bottom" />
<ListBox Name="listbox" ItemTemplate="{StaticResource my}" ItemsSource="{Binding}" ItemsPanel="{StaticResource threeRowPanel}" DockPanel.Dock="Top" />
</DockPanel>
</Window>
#3
Mark yi xia
#4
一下子加载1000多涨图片到内存,是不是有点浪费资源啊。
单击弹出一个窗体就能解决你的第二个问题
单击弹出一个窗体就能解决你的第二个问题