ItemsControl的两种数据绑定方式
最近在学习ItemsControl这个控件的时候,查看了MSDN上面的一个例子,并且自己做了一些修改,这里主要使用了两种方式来进行相应的数据绑定,一种是使用DataContext,另外一种是直接将一个类绑定到前台,其实这两种方式原理差不多都是将数据模型的对象添加到一个ObservableCollection集合中,然后再绑定到前台,下面分别介绍两种绑定方式:
第一种是将数据存储在一个ObservableCollection集合中,然后作为ItemsControl的DataContext对象,下面分别贴出相关的代码:
<Window x:Class="TestGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestGrid"
Title="MainWindow" Height="" Width="">
<Grid>
<ItemsControl Margin="" x:Name="myItemsControl" ItemsSource="{Binding}">
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<Border BorderBrush="Aqua" BorderThickness="" CornerRadius="">
<ItemsPresenter/>
</Border>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{ x:Type local:DataSource}">
<DataTemplate.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value=""/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</DataTemplate.Resources>
<Grid>
<Rectangle Fill="Green"/>
<Ellipse Fill="Red"/>
<StackPanel>
<TextBlock Margin="3,3,3,0" Text="{Binding Path=Priority,Mode=TwoWay}"/>
<TextBlock Margin="3,0,3,7" Text="{Binding Path=TaskName,Mode=TwoWay}"/>
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Control.Width" Value=""/>
<Setter Property="Control.Margin" Value=""/>
<Style.Triggers>
<Trigger Property="Control.IsMouseOver" Value="True">
<Setter Property="Control.ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Content.Description}"/>
</Trigger>
</Style.Triggers>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</Grid>
</Window>
这里需要注意的地方是 ItemsSource="{Binding}"这句必须添加,否则后台的数据是无法添加到前台的,这个需要注意,这里贴出后台的代码
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace TestGrid
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<DataSource> item = null;
public MainWindow()
{
InitializeComponent();
item = new ObservableCollection<DataSource>();
item.Add(
new DataSource()
{
Priority = "",
TaskName = "hello"
}
);
item.Add(
new DataSource()
{
Priority = "",
TaskName = "whats"
}
);
item.Add(
new DataSource()
{
Priority = "",
TaskName = "your"
}
);
item.Add(
new DataSource()
{
Priority = "",
TaskName = "name"
}
);
item.Add(
new DataSource()
{
Priority = "",
TaskName = "can"
}
);
item.Add(
new DataSource()
{
Priority = "",
TaskName = "you"
}
);
this.myItemsControl.DataContext = item; }
}
}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace TestGrid
{
public class DataSource : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public DataSource()
{
} private string priority;
public string Priority
{
get
{
return priority;
}
set
{
priority = value;
OnPropertyChanged("Priority");
}
} private string taskname;
public string TaskName
{
get
{
return taskname;
}
set
{
taskname = value;
OnPropertyChanged("TaskName");
}
} protected void OnPropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
} } private List<DataSource> datasources; public List<DataSource> DataSources
{
get { return datasources; }
set { datasources = value; }
} }
}
这里最重要的一句就是 this.myItemsControl.DataContext = item;这个是将刚才的集合绑定到ItemsControl控件上,这里需要我们的注意。
另外一种方式的数据绑定就是将一个类绑定到这个ItemsControl控件上,具体的方式如下:
<Window x:Class="TestGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestGrid"
Title="MainWindow" Height="" Width="">
<Window.Resources>
<local:MyData x:Key="myDataSource"/>
</Window.Resources>
<Grid>
<ItemsControl Margin="" x:Name="myItemsControl" ItemsSource="{Binding Source={StaticResource myDataSource}}">
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<Border BorderBrush="Aqua" BorderThickness="" CornerRadius="">
<ItemsPresenter/>
</Border>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{ x:Type local:DataSource}">
<DataTemplate.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value=""/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</DataTemplate.Resources>
<Grid>
<Rectangle Fill="Green"/>
<Ellipse Fill="Red"/>
<StackPanel>
<TextBlock Margin="3,3,3,0" Text="{Binding Path=Priority,Mode=TwoWay}"/>
<TextBlock Margin="3,0,3,7" Text="{Binding Path=TaskName,Mode=TwoWay}"/>
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Control.Width" Value=""/>
<Setter Property="Control.Margin" Value=""/>
<Style.Triggers>
<Trigger Property="Control.IsMouseOver" Value="True">
<Setter Property="Control.ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Content.Description}"/>
</Trigger>
</Style.Triggers>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</Grid>
</Window>
这里我们通过资源来引用一个类,让后使用 ItemsSource="{Binding Source={StaticResource myDataSource}}"将这个类绑定到ItemsSource控件上面,这里贴出相关的代码,我们定义了一个MyData的类,将该类作为数据源绑定到前台上,这个类的代码如下
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace TestGrid
{
public class MyData:ObservableCollection<DataSource>
{
public MyData()
{
Add (new DataSource()
{
Priority = "",
TaskName = "My"
}
);
Add(new DataSource()
{
Priority = "",
TaskName = "Name"
}
);
Add(new DataSource()
{
Priority = "",
TaskName = "Is"
}
);
Add(new DataSource()
{
Priority = "",
TaskName = "Ye"
}
);
Add(new DataSource()
{
Priority = "",
TaskName = "Bo"
}
); } }
}
这里面很重要的一部就是让这个类继承自 ObservableCollection<DataSource>,然后来添加相应的数据源,我们在使用继承的时候需要注意这些技巧。
其实这两种情况都是将一个数据集合绑定到前台,只不过是一些绑定的方式有所不同,实际的原理都是一样的!
ItemsControl的两种数据绑定方式的更多相关文章
- highcharts.js两种数据绑定方式和异步加载数据的使用
一,我们先来看看异步加载数据的写法(这是使用MVC的例子) 1>js写法 <script src="~/Scripts/jquery-2.1.4.min.js"> ...
- Web APi之认证(Authentication)两种实现方式【二】(十三)
前言 上一节我们详细讲解了认证及其基本信息,这一节我们通过两种不同方式来实现认证,并且分析如何合理的利用这两种方式,文中涉及到的基础知识,请参看上一篇文中,就不再叙述废话. 序言 对于所谓的认证说到底 ...
- Android中BroadcastReceiver的两种注册方式(静态和动态)详解
今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
- JavaScript 函数的两种声明方式
1.函数声明的方式 JavaScript声明函数有两种选择:函数声明法,表达式定义法. 函数声明法 function sum (num1 ,num2){ return num1+num2 } 表达式定 ...
- Redis两种持久化方式(RDB&AOF)
爬虫和转载请注明原文地址;博客园蜗牛:http://www.cnblogs.com/tdws/p/5754706.html Redis所需内存 超过可用内存怎么办 Redis修改数据多线程并发—Red ...
- struts2+spring的两种整合方式
也许有些人会因为学习了struts1,会以为struts2.struts1与spring的整合也是一样的,其实这两者相差甚远.下面就来讲解一下struts2与spring的整合两种方案.(部分转载,里 ...
- easyui datagride 两种查询方式
easyui datagride 两种查询方式function doReseach() { //$('#tt').datagrid('load', { // FixedCompany: $('.c_s ...
- 【Visual Lisp】两种出错处理方式
两种出错处理方式:一种是对出错函数进行重定义,一种是对错误进行捕捉处理. ;;============================================================= ...
随机推荐
- Java面试题复习之Java基础
1.面向对象的特征主要有哪些? 封装.继承.多态.抽象 2.final.finally.finalize的区别? final主要用于修饰类.方法.属性(变量)等. 通常被final修饰的类不能够被 ...
- MATLAB 图形着色
1.matlab中的颜色查找表函数: (1)autumn:从红色向橘黄色.黄色平稳过渡: (2)bone:为含有较高的蓝色组分的gray颜色查找表: (3)colorcube:包含RGB颜色空间中尽可 ...
- nightwatch-前端自动化测试工具安装
最近再弄这个前端自动化测试工具,刚开始弄了几天,目前为止遇到很多坑,光是安装就费了不少时间,记录一下,以便自己忘记. 这里是它的官网,目前没找到中文版的官网,全英文,对我这个英语渣来说有点难理解. 一 ...
- 笔记二:常用的h5语义化标签
0.前言: 所谓语义化标签就是一种 我们仅通过标签名就能判断出该标签内容的语义的标签,见名知意. 总结这部分内容,主要是为了能从繁琐的div嵌套div中,改成带有h5标签码.这样更有利于读写代码. 人 ...
- Linux kernel 之 socket 创建过程分析
重要结构体 struct socket 结构体 // 普通的 BSD 标准 socket 结构体 // socket_state: socket 状态, 连接?不连接? // type: socket ...
- Java并发(六)线程池监控
目录 一.线程池监控参数 二.线程池监控类 三.注意事项 在上一篇博文中,我们介绍了线程池的基本原理和使用方法.了解了基本概念之后,我们可以使用 Executors 类创建线程池来执行大量的任务,使用 ...
- 【Codeforces 331D3】Escaping on Beaveractor
题意:给\(b\times b\)的网格,其中有\(n\)个不交叉的箭头. 现在有\(q\)个询问,每个询问包含一个点\((x,y)\),以及一个方向\(dir\).时间\(t\). 要求从\((x, ...
- Promise使用时应注意的问题
最近在使用axios库时遇到了个问题,后端接口报了500错误,但前端并未捕获到.1. 调用接口的业务代码如下: // 业务代码调用 axios({ url: url, method: 'post', ...
- [转]curl的错误代码
转贴者按: 今天在使用curl的时候碰到了一个错误,如下所示: External Program Failed: D:\Tools\curl\curl.exe (return code was 18) ...
- .net mvc数据库操作添加数据的几中方法
1. Defining sets on a derived context 1) DbSet属性:指定集合为Entity类型 2) IDbSet属性 3) 只读set属性 public IDbSet& ...