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. Intellij IDEA 使用Debug模式运行非常慢

    今天在用Debug的时候,idea运行非常慢,搜了一下有人说: 自己检查发现果然如此,把在方法前的断点去掉(移到方法体内),就正常了.

  2. JS的replace方法【转】

    replace() 方法的参数 replacement 可以是函数而不是字符串.在这种情况下,每个匹配都调用该函数,它返回的字符串将作为替换文本使用.该函数的第一个参数是匹配模式的字符串.接下来的参数 ...

  3. JQUERY 动态时钟

    <script type="text/javascript" src="script/jquery-1.8.3.min.js"></scrip ...

  4. SqlServer之游标深入

    原创文章,转载必需注明出处:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/introduce-for-sqlserver-curs ...

  5. 嵌入式开发笔记 - U-Boot相关

    1.U-boot使用准备 1.1 U-boot下载 通过德国的denx软件中心提供的FTP下载合集,下载网址: ftp://ftp.denx.de/pub/u-boot/

  6. spring+hibernate管理多个数据源(非分布式事务)

    本文通过一个demo,介绍如何使用spring+hibernate管理多个数据源,注意,本文的事务管理并非之前博文介绍的分布式事务. 这个demo将使用两个事务管理器分别管理两个数据源.对于每一个独立 ...

  7. Animated App Boot Example : Fastest animation at app boot time

    This iPhone app shows how to create an animation that is displayed when the app starts. The animatio ...

  8. Mac电脑svn使用经验,Mac电脑使用android studio使用经验

    1.先查看mac本身是否自带java1.6 没有去appstore里面安装 2.http://www.android-studio.org/   下载mac版的ide 3.安装完成ide后,打开and ...

  9. sql常识-Alias

    SQL Alias 表的 SQL Alias 语法 SELECT column_name(s) FROM table_name AS alias_name 列的 SQL Alias 语法 SELECT ...

  10. Cocos2d-x开发实例:单点触摸事件

    下面我们通过一个实例详细了解一下,层中单点触摸事件的实现过程.感受一下它的缺点和优点.该实例场景如下图所示,场景中有两个方块精灵,我们可以点击和移动它们.   下面我们看看HelloWorldScen ...