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】两种出错处理方式
两种出错处理方式:一种是对出错函数进行重定义,一种是对错误进行捕捉处理. ;;============================================================= ...
随机推荐
- Cookie、sessionStorage与localStorage的区别
(1) sessionStorage 保存数据的方法: SessionStor.setItem(“key”,”value”) 或者写成 sessionStorage.key=”value” 读取数据的 ...
- csvwrite
https://ww2.mathworks.cn/help/matlab/ref/csvwrite.html
- jmeter(十八)关联之XPath Extractor
之前的博客,有介绍jmeter如何对请求进行关联的一种常见用法,即:后置处理器中的正则表达式提取器,下面介绍另一种关联方法,XPath Extractor! 所谓关联,从业务角度讲,即:某些操作步骤与 ...
- (转)用graph-easy描绘kubenetes描绘k8s组件逻辑图
1.参考: https://xuxinkun.github.io/2018/09/03/graph-easy/ http://bloodgate.com/perl/graph/manual/faq.h ...
- eaeyui-combobox实现组合查询(即实现多个值得搜索)
2015年9月1日,今天要实现下拉框的组合查询功能,即可以再下拉框中选择多个值,输入框中每个值之间有逗号隔开,传到后台,由split函数将其分割开,组合成数组,在由sql查询. 实现的效果是: 当时在 ...
- Vue-详解设置路由导航的两种方法: <router-link :to="..."> 和router.push(...)
一.<router-link :to="..."> to里的值可以是一个字符串路径,或者一个描述地址的对象.例如: // 字符串 <router-link to= ...
- 一个可爱 & 小清新的加载等待Android控件
https://github.com/Carson-Ho/Kawaii_LoadingView
- LiveCharts文档-4基本绘图-1基本线条图
原文:LiveCharts文档-4基本绘图-1基本线条图 4基本绘图-1基本线条图 using System; using System.Windows.Forms; using System.Win ...
- koa2入门(2) koa-router 路由处理
项目地址:https://github.com/caochangkui/demo/tree/koa-test 1. 创建项目 创建目录 koa-test npm init 创建 package.jso ...
- dp方法论——由矩阵相乘问题学习dp解题思路
前篇戳:dp入门——由分杆问题认识动态规划 导语 刷过一些算法题,就会十分珍惜“方法论”这种东西.Leetcode上只有题目.讨论和答案,没有方法论.往往答案看起来十分切中要害,但是从看题目到得到思路 ...