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布局的更多相关文章

  1. WPF中Grid布局

    WPF中Grid布局XMAl与后台更改,最普通的登录界面为例. <Grid Width="200" Height="100" > <!--定义 ...

  2. [转]使用CSS3 Grid布局实现内容优先

    使用CSS3 Grid布局实现内容优先  http://www.w3cplus.com/css3/css3-grid-layout-module.html 本文由大漠根据Rachel Andrew的& ...

  3. wpf后置代码中的Grid布局以及图片路径的设置

    之前用Grid练习连连看布局时,遇到了几个困惑.此次就把当时的一些收获写出来,供以后翻看. 图片路径可能比较常用,所以就写在第一个了. 在xaml中,设置图片非常简单,只要把图片拷贝到资源目录(这里假 ...

  4. css grid布局的首次使用

    首先来看一下效果图 接下来废话不多说,先上代码 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  5. 学习ExtJS的grid布局

    这是之前学习ExtJS布局的时候我导师让我重点熟悉的内容.之后会发一个最近写的结合MVC项目的grid布局的案例. 上一篇关于ExtJS的学习资料什么的都已经更在上一篇了,这里只是对一些代码的记录. ...

  6. CSS字体渐变 & 隐藏浏览器滚动条 & grid布局(转载)

    字体渐变  https://www.zhangxinxu.com/study/201104/css3-text-gradient-2.html 隐藏浏览器滚动条  https://blog.csdn. ...

  7. CSS Grid 布局完全指南(图解 Grid 详细教程)

    CSS Grid 布局是 CSS 中最强大的布局系统.与 flexbox 的一维布局系统不同,CSS Grid 布局是一个二维布局系统,也就意味着它可以同时处理列和行.通过将 CSS 规则应用于 父元 ...

  8. 快速使用CSS Grid布局,实现响应式设计

    常用Grid布局属性介绍 下面从一个简单Grid布局例子说起. CSS Grid 布局由两个核心组成部分是 wrapper(父元素)和 items(子元素). wrapper 是实际的 grid(网格 ...

  9. 补发————grid布局

    CSS Grid布局是CSS中最强大的布局系统.与flexbox的一位布局不同的是CSS Grid布局是一个二维布局系统,即它可以同时处理列和行.通过将CSS规则应用于父元素和其子元素,就可以轻松使用 ...

随机推荐

  1. B. Little Dima and Equation

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  2. Python学习笔记 第二课 循环

    >>> movies=["The Holy Grail", 1975, "The Life of Brian", 1979, "Th ...

  3. CentOS(九)--与Linux文件和目录管理相关的一些重要命令①

       接上一篇文章,实际生产过程中的目录管理一定要注意用户是root 还是其他用户. 一.目录与路径 1.相对路径与绝对路径 因为我们在Linux系统中,常常要涉及到目录的切换,所以我们必须要了解 & ...

  4. GIT免密码PUSH

    摘自:dudu 备忘,感谢! 1. 在Windows中添加一个HOME环境变量,值为%USERPROFILE%,如下图: 2. 在“开始>运行”中打开%Home%,新建一个名为“_netrc”的 ...

  5. poj 2823 单调队列

    思路:裸的单调队列. #include<iostream> #include<cstring> #include<cstdio> #include<algor ...

  6. Bootstrap简单Demo(2015年05月-18日)

    Bootstrap的简单使用 1.Bootstrap是什么? 这是Bootstrap官网上对它的描述:Bootstrap是最受欢迎的HTML.CSS和JS框架,用于开发响应式布局.移动设备优先的WEB ...

  7. IE升级代码时邮件内容

    TypeErrorUnable to set property 'value' of undefined or null reference. 但是可以进入添加页面,填完信息后,submit后跳转至 ...

  8. 关于Tesseract3.01的使用方法

    Tesseract就不多介绍勒,能找到的人都知道是干嘛的 下面记录一下C# vs2010下的使用方法(借鉴http://blog.csdn.net/bobo1013767522/article/det ...

  9. WCF Membership and Role Provider

    本文介绍的是如何使用Membership 和 Role Provider 来控制 WCF 调用方法的权限. 比如我们有一个WCF Method 叫 GetData(int num),然后我们只允许Ro ...

  10. 删除mssqlserver表数据,使id从0开始

    ********************************* 注意备份好数据! *************************** 1.删除表数据 delete 表名 2.执行 dbcc c ...