最近在学习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的两种数据绑定方式的更多相关文章

  1. highcharts.js两种数据绑定方式和异步加载数据的使用

    一,我们先来看看异步加载数据的写法(这是使用MVC的例子) 1>js写法 <script src="~/Scripts/jquery-2.1.4.min.js"> ...

  2. Web APi之认证(Authentication)两种实现方式【二】(十三)

    前言 上一节我们详细讲解了认证及其基本信息,这一节我们通过两种不同方式来实现认证,并且分析如何合理的利用这两种方式,文中涉及到的基础知识,请参看上一篇文中,就不再叙述废话. 序言 对于所谓的认证说到底 ...

  3. Android中BroadcastReceiver的两种注册方式(静态和动态)详解

    今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...

  4. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

  5. JavaScript 函数的两种声明方式

    1.函数声明的方式 JavaScript声明函数有两种选择:函数声明法,表达式定义法. 函数声明法 function sum (num1 ,num2){ return num1+num2 } 表达式定 ...

  6. Redis两种持久化方式(RDB&AOF)

    爬虫和转载请注明原文地址;博客园蜗牛:http://www.cnblogs.com/tdws/p/5754706.html Redis所需内存 超过可用内存怎么办 Redis修改数据多线程并发—Red ...

  7. struts2+spring的两种整合方式

    也许有些人会因为学习了struts1,会以为struts2.struts1与spring的整合也是一样的,其实这两者相差甚远.下面就来讲解一下struts2与spring的整合两种方案.(部分转载,里 ...

  8. easyui datagride 两种查询方式

    easyui datagride 两种查询方式function doReseach() { //$('#tt').datagrid('load', { // FixedCompany: $('.c_s ...

  9. 【Visual Lisp】两种出错处理方式

    两种出错处理方式:一种是对出错函数进行重定义,一种是对错误进行捕捉处理. ;;============================================================= ...

随机推荐

  1. 作业二/Git的安装以及使用

    作业要求来自https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 GitHub地址 https://github.com/20160 ...

  2. flask 路由和视图

    路由设置的俩种方式 @app.route('/xxx') def index(): return 'index' ------------------------------------------ ...

  3. AliOS-Things linkkitapp解读

    app-example-linkkitapp是AliOS-Things提供的设备联网并且和阿里云IOT平台数据交互的一个示例程序: 1:application_start()程序在app_entry. ...

  4. 关于window.onload()的加载问题

    今天写了个js的demo,遇到了个小问题,后来发现是自己对window.onload()的具体用处不是太清楚,现在跟大家分享一下. <head> <meta http-equiv=& ...

  5. oracle 把查询结果插入到表中几种方式

    转载:Oracle中把一个查询结果插入到一张表中 以下是信息留存: 一.Oracle数据库中,把一张表的查询结果直接生成并导入一张新表中. 例如:现有只有A表,查询A表,并且把结果导入B表中.使用如下 ...

  6. [07] 使用注解完成IOC配置

    1.扫描配置 之前使用的Spring的Bean管理都是通过xml的配置文件来操作的,在Spring3.0之后已经引入了注解形式,Spring可以在指定路径下进行扫描,寻找标注了@Component.@ ...

  7. 在项目中,多个方法会调用相同的sql语句,怎么解决各个方法的不同sql查询,解决冲突。

    公司的代码中sql语句,可能会被多个方法进行调用,但是有的方法会关联到别的表,这样的话,如果修改不当,那么同样调用该sql语句的方法,会出现报错. 最近做的公司的一个功能,就出现这样一个问题,虽然本功 ...

  8. [Spark][Hive]Hive的命令行客户端启动:

    [Spark][Hive]Hive的命令行客户端启动: [training@localhost Desktop]$ chkconfig | grep hive hive-metastore 0:off ...

  9. 使用TensorFlow的递归神经网络(LSTM)进行序列预测

    本篇文章介绍使用TensorFlow的递归神经网络(LSTM)进行序列预测.作者在网上找到的使用LSTM模型的案例都是解决自然语言处理的问题,而没有一个是来预测连续值的. 所以呢,这里是基于历史观察数 ...

  10. SSL踩坑ERR_SSL_VERSION_OR_CIPHER_MISMATCH

    最近公司项目开发了一个微信小程序,并且部署测试OK,由于微信小程序调用的后端接口必须是HTTPS,所以给接口安装了SSL,第一天测试都正常.第二天早上再使用时页面无响应. 抓包发现是后端接口抛出: n ...