Convert和RelativeSource
自定义Converter
后台Converter类实现接口IValueConverter
方法Convert是值->UI
方法ConvertBack是UI->值
初始化走Convert
public class ColorConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
//value为当前的对象
var item = value as ListViewItem;
var view = ItemsControl.ItemsControlFromItemContainer(item);
var index = view.ItemContainerGenerator.IndexFromContainer(item);
var data = view.Items[index] as Student;
if (data.Age == 22)
return Brushes.Red;
if (data.Age % 2 == 0)
return Brushes.Pink;
else
return Brushes.DeepSkyBlue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
前端
前端引用命名空间,实例化静态资源
<converts:ColorConvert x:Key="MyConvert"></converts:ColorConvert>
将MyConvert绑定给Style
<Style x:Key="Item" TargetType="ListViewItem">
<!--表明将自身的背景颜色修改-->
<Setter Property="Background">
<Setter.Value>
<!--RelativeSource="{RelativeSource Self}"的Self不能用控件名称代替-->
<Binding RelativeSource="{RelativeSource Self}" Converter="{StaticResource MyConvert}"></Binding>
</Setter.Value>
</Setter>
</Style>
使用Style
<!--使用ItemContainerStyle将Style赋给ListView-->
<ListView ItemsSource="{Binding }" ItemContainerStyle="{StaticResource Item}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="姓名" DisplayMemberBinding="{Binding Name}"></GridViewColumn>
<GridViewColumn Header="年龄" DisplayMemberBinding="{Binding Age}"></GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
示例代码
https://github.com/zLulus/NotePractice/tree/dev3/WPF/WpfDemo/Converts
Convert和RelativeSource的更多相关文章
- WPF学习目录
基本概念 数据源Source-目标Target WPF生命周期 App.xaml 依赖属性 WPF路由 线程操纵UI问题 利用属性中设置.查看DataContext/Command等 分析布局 写数据 ...
- [WPF系列-高级TemplateBinding vs RelativeSource TemplatedParent]
What is the difference between these 2 bindings: <ControlTemplate TargetType="{x:Type Button ...
- 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数据转换
[源码下载] 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数 ...
- Convert BSpline Curve to Arc Spline in OpenCASCADE
Convert BSpline Curve to Arc Spline in OpenCASCADE eryar@163.com Abstract. The paper based on OpenCA ...
- Convert.ToInt32()、int.Parse()和(int)三者的区别
Convert.ToInt32将object类类型转换成int类型,如Convert.ToInt32(session["shuzi"]); (int)适合简单数据类型之间的转换: ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] Convert a Number to Hexadecimal 数字转为十六进制
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
- [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
随机推荐
- Fragment之一:Fragment入门 分类: H1_ANDROID 2013-11-15 18:16 2799人阅读 评论(2) 收藏
参考自张泽华视频 Fragment是自Android3.0后引入的特性,主要用于在不同的屏幕尺寸中展现不同的内容. Fragment必须被嵌入Activity中使用,总是作为Activity的组成部分 ...
- winscp ppk无需密码登录(失败)
http://blog.csdn.net/catoop/article/details/8284803 按上文将Linux下生成的密钥文件id_rsa通过puttygen生成对应的.ppk文件,用wi ...
- js获取浏览器尺寸
Javascript: alert(document.body.clientWidth); //网页可见区域宽(body) alert(document.body.clientHeigh ...
- poj 2965 The Pilots Brothers' refrigerator
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18040 ...
- [Typescript] What is a Function Type ? Function Types and Interfaces - Are They Related ?
Function Type: type messageFn = (name: string) => string; function sayHello(name: string): string ...
- [Ramda] Difference between R.converge and R.useWith
So what is Point-free function. Take a look this example: const getUpdatedPerson = (person) => R. ...
- [Angular] Dynamic component's instance and sorting
After create a component dynamic, we are able to change the component's props and listen to its even ...
- Silverlight三维透视+倒影效果
原文:Silverlight三维透视+倒影效果 知识概要: 1.使用2D内容创建3D体验,了解图像的PlaneProjection属性,具体内容读者自己查看文档. 2.Silverlight图形图形的 ...
- 建立一个OTP应用
http://www.javaeye.com/topic/374167 以下是在erlang项目开发中的一些记录,即包含很多通俗易懂的原则,也包含一些似是而非的建议,比较混乱,还没有积累到一个可以分门 ...
- 前端css实现最基本的时间轴
原型: 图片.png 代码: <!DOCTYPE html > <html> <head> <link rel="stylesheet" ...