ItemsControl 使用Grid布局
ItemsControl控件经常用到,在ItemsPanel里大多是StackPanel,WrapPanel,以下项目演示如何使用Grid用于ItemsControl布局
1.先看运行效果

2.xaml代码如下
<Window x:Class="GridHelperDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:GridHelperDemo="clr-namespace:GridHelperDemo"
Title="MainWindow"
Height=""
Width="">
<Window.DataContext>
<GridHelperDemo:StatisticItemList>
<GridHelperDemo:StatisticItem Index=""
Name="张三"
Value="" />
<GridHelperDemo:StatisticItem Index=""
Name="李四"
Value="" />
<GridHelperDemo:StatisticItem Index=""
Name="老王"
Value="" />
<GridHelperDemo:StatisticItem Index=""
Name="小李"
Value="" />
<GridHelperDemo:StatisticItem Index=""
Name="大强"
Value="" />
<GridHelperDemo:StatisticItem Index=""
Name="崔颢"
Value="" />
<GridHelperDemo:StatisticItem Index=""
Name="明月"
Value="" />
<GridHelperDemo:StatisticItem Index=""
Name="阿辉"
Value="" />
</GridHelperDemo:StatisticItemList>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition />
<RowDefinition Height="auto" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock HorizontalAlignment="Center"
FontSize=""
Text="根据ItemsSource生成行" />
<!--根据ItemsSource生成行-->
<ItemsControl ItemsSource="{Binding}"
Grid.Row="">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
51 <Grid GridHelperDemo:GridHelper.RowsCount="{Binding Count}">
52 </Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions> <Border BorderBrush="Black"
BorderThickness="">
<TextBlock Text="{Binding Name}" />
</Border> <Border BorderBrush="Black"
BorderThickness=""
Grid.Column="">
<TextBlock Text="{Binding Value}" />
</Border>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
76 <ItemsControl.ItemContainerStyle>
77 <Style>
78 <Setter Property="Grid.Row"
79 Value="{Binding Index}" />
80 </Style>
81 </ItemsControl.ItemContainerStyle>
</ItemsControl> <TextBlock HorizontalAlignment="Center"
FontSize=""
Grid.Row=""
Text="根据ItemsSource生成列" />
<!--根据ItemsSource生成列-->
<ItemsControl ItemsSource="{Binding}"
Grid.Row="">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
94 <Grid GridHelperDemo:GridHelper.ColumnsCount="{Binding Count}">
95 </Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions> <Border BorderBrush="Black"
BorderThickness="">
<TextBlock Text="{Binding Name}" />
</Border> <Border BorderBrush="Black"
BorderThickness=""
Grid.Row="">
<TextBlock Text="{Binding Value}" />
</Border>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
119 <ItemsControl.ItemContainerStyle>
120 <Style>
121 <Setter Property="Grid.Column"
122 Value="{Binding Index}" />
123 </Style>
124 </ItemsControl.ItemContainerStyle>
</ItemsControl>
</Grid>
</Window>
3 .cs代码如下
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
} /// <summary>
/// 统计项
/// </summary>
public class StatisticItem
{
public int Index { get; set; } public string Name { get; set; } public int Value { get; set; }
} /// <summary>
/// 统计项列表
/// </summary>
public class StatisticItemList : List<StatisticItem>
{ }
4.GridHelper.Cs定义
public class GridHelper
{
#region RowsCount 附加属性
public static readonly DependencyProperty RowsCountProperty =
DependencyProperty.RegisterAttached("RowsCount", typeof(int), typeof(GridHelper), new PropertyMetadata(1, RowsCountPropertyChangedCallback)); public static void SetRowsCount(UIElement element, int value)
{
element.SetValue(RowsCountProperty, value);
} public static int GetRowsCount(UIElement element)
{
return (int)element.GetValue(RowsCountProperty);
} public static void RowsCountPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
int count = Convert.ToInt32(e.NewValue);
if (sender is Grid && count > 0)
{
Grid g = sender as Grid;
g.RowDefinitions.Clear();
for (int i = 0; i < count; i++)
{
g.RowDefinitions.Add(new RowDefinition());
}
}
}
#endregion #region ColumnsCount 附加属性
public static readonly DependencyProperty ColumnsCountProperty =
DependencyProperty.RegisterAttached("ColumnsCount", typeof(int), typeof(GridHelper), new PropertyMetadata(1, ClumnsCountPropertyChangedCallback)); public static void SetColumnsCount(UIElement element, int value)
{
element.SetValue(ColumnsCountProperty, value);
} public static int GetColumnsCount(UIElement element)
{
return (int)element.GetValue(ColumnsCountProperty);
} public static void ClumnsCountPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
int count = Convert.ToInt32(e.NewValue);
if (sender is Grid)
{
Grid g = sender as Grid;
g.ColumnDefinitions.Clear();
for (int i = 0; i < count; i++)
{
g.ColumnDefinitions.Add(new ColumnDefinition());
}
}
}
#endregion
}
ItemsControl 使用Grid布局的更多相关文章
- WPF中Grid布局
WPF中Grid布局XMAl与后台更改,最普通的登录界面为例. <Grid Width="200" Height="100" > <!--定义 ...
- [转]使用CSS3 Grid布局实现内容优先
使用CSS3 Grid布局实现内容优先 http://www.w3cplus.com/css3/css3-grid-layout-module.html 本文由大漠根据Rachel Andrew的& ...
- wpf后置代码中的Grid布局以及图片路径的设置
之前用Grid练习连连看布局时,遇到了几个困惑.此次就把当时的一些收获写出来,供以后翻看. 图片路径可能比较常用,所以就写在第一个了. 在xaml中,设置图片非常简单,只要把图片拷贝到资源目录(这里假 ...
- css grid布局的首次使用
首先来看一下效果图 接下来废话不多说,先上代码 <!DOCTYPE html> <html lang="en"> <head> <meta ...
- 学习ExtJS的grid布局
这是之前学习ExtJS布局的时候我导师让我重点熟悉的内容.之后会发一个最近写的结合MVC项目的grid布局的案例. 上一篇关于ExtJS的学习资料什么的都已经更在上一篇了,这里只是对一些代码的记录. ...
- CSS字体渐变 & 隐藏浏览器滚动条 & grid布局(转载)
字体渐变 https://www.zhangxinxu.com/study/201104/css3-text-gradient-2.html 隐藏浏览器滚动条 https://blog.csdn. ...
- CSS Grid 布局完全指南(图解 Grid 详细教程)
CSS Grid 布局是 CSS 中最强大的布局系统.与 flexbox 的一维布局系统不同,CSS Grid 布局是一个二维布局系统,也就意味着它可以同时处理列和行.通过将 CSS 规则应用于 父元 ...
- 快速使用CSS Grid布局,实现响应式设计
常用Grid布局属性介绍 下面从一个简单Grid布局例子说起. CSS Grid 布局由两个核心组成部分是 wrapper(父元素)和 items(子元素). wrapper 是实际的 grid(网格 ...
- 补发————grid布局
CSS Grid布局是CSS中最强大的布局系统.与flexbox的一位布局不同的是CSS Grid布局是一个二维布局系统,即它可以同时处理列和行.通过将CSS规则应用于父元素和其子元素,就可以轻松使用 ...
随机推荐
- Columbus’s bargain
Columbus’s bargain Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdfs: 一个分布式文件系统(一)
一. hdfs设计的动机 为大规模分布式计算准备的分布式文件系统,并非实时性要求很高的文件系统. 二. 设计的取舍 1. 因为要求有高吞吐量,所以牺牲读取文件的实时性,实时性要求高的分布式文件系统可以 ...
- Java基础知识强化101:Java 中的 String对象真的不可变吗 ?
1. 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变的对象呢? 可以这样认为:如果一个对象,在它创建完成之后,不能再改变它的状态,那么这个对 ...
- request 和response 中的setCharacterEncoding区别
response和request的setCharacterEncoding 一.request.setCharacterEncoding():是设置从request中取得的值或从数据库中取出的值. 指 ...
- CentOS-6.5安装配置Tomcat7
一.下载tomcat cd /usr/local/src wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.50/bin/apache ...
- 跟我一起学习VIM - The Life Changing Editor
前两天同事让我在小组内部分享一下VIM,于是我花了一点时间写了个简短的教程.虽然准备有限,但分享过程中大家大多带着一种惊叹的表情,原来编辑器可以这样强大,这算是对我多年来使用VIM的最大鼓舞吧.所以分 ...
- COGS 265 线段覆盖
265. 线段覆盖 ★★☆ 输入文件:xdfg.in 输出文件:xdfg.out 简单对比时间限制:2 s 内存限制:20 MB [问题描述] 有一根长度为 L 的白色条状物.有两种操 ...
- H5-xhtml+css2-静态百度首页练习
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- .net System.Net.Mail 之用SmtpClient发送邮件 Demo
private static bool sendMail() { try { //接收人邮箱 string SendTo = "XXXXX@163.com"; //抄送人邮箱 st ...
- sql常识-top
TOP 子句 TOP 子句用于规定要返回的记录的数目. 对于拥有数千条记录的大型表来说,TOP 子句是非常有用的. 注释:并非所有的数据库系统都支持 TOP 子句. SQL Server 的语法: S ...