原文 获取listboxitem在ListBox中的index并转换成abcd

截图如下:

1、实现Converter  获取到listbox,并得到listitem在listbox中的index

 public class ItemContainerToZIndexConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var itemContainer = (DependencyObject)value; var itemsControl = Tool.FindAncestor<ListBox>(itemContainer);
int index = itemsControl.ItemContainerGenerator.IndexFromContainer(itemContainer);
switch (index)
{
case : return "A";
case : return "B";
case : return "C";
case : return "D";
}
return null;
} public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException(); }
} public static class Tool
{
public static T FindAncestor<T>(this DependencyObject obj) where T : DependencyObject
{
var tmp = VisualTreeHelper.GetParent(obj);
while (tmp != null && !(tmp is T))
{
tmp = VisualTreeHelper.GetParent(tmp);
} return (T)tmp;
}
}

2、<Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem">

使用数据绑定<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource converter}}"/>

 <Window.Resources>
<local:ItemContainerToZIndexConverter x:Key="converter" />
</Window.Resources>
 <Style x:Key="ListBoxItemStyle1"
TargetType="ListBoxItem">
<Setter Property="Background"
Value="Transparent" />
<Setter Property="BorderThickness"
Value="0" />
<Setter Property="BorderBrush"
Value="Transparent" />
<Setter Property="Padding"
Value="0" />
<Setter Property="HorizontalContentAlignment"
Value="Left" />
<Setter Property="VerticalContentAlignment"
Value="Top" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border x:Name="LayoutRoot"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
VerticalAlignment="{TemplateBinding VerticalAlignment}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver" />
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background"
Storyboard.TargetName="LayoutRoot">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource TransparentBrush}" />
</ObjectAnimationUsingKeyFrames>
<DoubleAnimation Duration="0"
To=".5"
Storyboard.TargetProperty="Opacity"
Storyboard.TargetName="ContentContainer" />
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground"
Storyboard.TargetName="ContentContainer">
<DiscreteObjectKeyFrame KeyTime="0"
Value="White"></DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Selected">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground"
Storyboard.TargetName="ContentContainer">
<DiscreteObjectKeyFrame KeyTime="0"
Value="Blue" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource converter}}" />
<ContentControl Grid.Column="1"
x:Name="ContentContainer"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
Foreground="{TemplateBinding Foreground}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

3、ListBox中 ItemContainerStyle="{StaticResource ListBoxItemStyle1}

获取listboxitem在ListBox中的index并转换成abcd的更多相关文章

  1. C# 中怎么将string转换成int型

    int intA = 0;1.intA =int.Parse(str);2.int.TryParse(str, out intA);3.intA = Convert.ToInt32(str);以上都可 ...

  2. Linux c字符串中不可打印字符转换成16进制

    本文由 www.169it.com 搜集整理 如果一个C字符串中同时包含可打印和不可打印的字符,如果想将这个字符串写入文件,同时方便打开文件查看或者在控制台中打印出来不会出现乱码,那么可以将字符串中的 ...

  3. Python3 将configparser从ini文件中读取的内容转换成字典格式

    因为写脚本的用到了,所以研究了下怎么将configparser从ini文件中读取的内容转换成字典格式. 整理一下,希望能对大家有帮助. 从http://stackoverflow.com/questi ...

  4. java中如何把图片转换成二进制流的代码

    在学习期间,把开发过程经常用到的一些代码段做个备份,下边代码内容是关于java中如何把图片转换成二进制流的代码,应该能对各朋友也有用处. public byte[] SetImageToByteArr ...

  5. shell中把大写字母转换成小写字母

    shell中把大写字母转换成小写字母 参考:http://www.jb51.net/article/40257.htm echo "AABBCC" | tr "[:upp ...

  6. WPF中实现图片文件转换成Visual对象,Viewport3D对象转换成图片

    原文:WPF中实现图片文件转换成Visual对象,Viewport3D对象转换成图片 1.图片文件转换成Visual对象 private Visual CreateVisual(string imag ...

  7. python 练习题:将列表中的大写字母转换成小写

    将列表中的大写字母转换成小写如果list中既包含字符串,又包含整数,由于非字符串类型没有lower()方法,L1 = ['Hello', 'World', 18, 'Apple', None]请修改列 ...

  8. C# : 操作Word文件的API - (将C# source中的xml注释转换成word文档)

    这篇博客将要讨论的是关于: 如何从C#的source以及注释, 生成一份Word格式的关于各个类,函数以及成员变量的说明文档. 他的大背景如下...... 最近的一个项目使用C#, 分N个模块, 在项 ...

  9. js获取时间加多山天和时间戳转换成日期

    function huoqu(){    var data = $("#data").val();//获取的时间    var day = $('#day').val();//往后 ...

随机推荐

  1. HTML中Id和Name的区别

    源地址:http://www.cnblogs.com/laodai/articles/2244215.html 在html中:name指的是用户名称,ID指的是用户注册是系统自动分配给用户的一个序列号 ...

  2. 在bmp上添加字符

    //打开位图文件,得到位图句柄   HBITMAP OpenBmpFile(HDC hDC, LPSTR lpszFileName)   {       HBITMAP hBmp = NULL;    ...

  3. ORALCE 之LRU链与脏LRU链【转载】

    今天是2013-09-09,时别n久的一篇经典文章,有被我在google发现了,再次转载一下.学习一下. 一.LRU链: 任何缓存的大小都是有限制的,并且总不如被缓存的数据多.就像Buffer cac ...

  4. js技术要点---JS 获取网页源代码

    JS 获取网页源代码 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html& ...

  5. STM32学习之路-SysTick的应用(时间延迟)

    开发板:奋斗V5 好~ 菜B要来搞实验了.. 前面已经说了SysTick的工作原理什么的了,这里就不说了.. 先来做第一个实验: 盗自奋斗的样例,嘿嘿, 用SysTick产生1ms的基准时间,产生中断 ...

  6. ZooKeeper安装与运行

    ZooKeeper安装与运行 首先从官网下载ZooKeeper压缩包,然后解压下载得到的ZooKeeper压缩包,发现有“bin,conf,lib”等目录.“bin目录”中存放有运行脚本:“conf目 ...

  7. 浅谈 PHP 变量可用字符

    原文:浅谈 PHP 变量可用字符 先来说说php变量的命名规则,百度下一抓一大把:(1) PHP的变量名区分大小写;(2) 变量名必须以美元符号$开始;(3) 变量名开头可以以下划线开始;(4) 变量 ...

  8. java 获取当期时间之前几小时的时间

    Calendar expireDate = Calendar.getInstance(); expireDate.set(Calendar.HOUR_OF_DAY, expireDate.get(Ca ...

  9. zoj2760(最大流)

    传送门:How Many Shortest Path 题意:给出n个点,和n*n的矩阵表示有向图.a[i][j]为-1表示i到j没有路径:不为-1则表示i到j的路径长度.给出一个vs和vt,要求vs到 ...

  10. UVa 213 Message Decoding(World Finals1991,串)

     Message Decoding  Some message encoding schemes require that an encoded message be sent in two part ...