截图如下:

      

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 0:
return "A";
case 1:
return "B";
case 2:
return "C";
case 3:
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、自定义CheckAbleItem继承ListBoxItem,并在VisualStateGroup中的Selected加入False、Right两个状态:

[TemplateVisualState(Name = CheckAbleItem.FalseVisualState, GroupName = CheckAbleItem.SelectedVisualStateGroup)]
[TemplateVisualState(Name = CheckAbleItem.RightVisualState, GroupName = CheckAbleItem.SelectedVisualStateGroup)]
public class CheckAbleItem : ListBoxItem
{
private const string SelectedVisualStateGroup = "Selected"; private const string FalseVisualState = "False";
private const string RightVisualState = "Right"; public void Check(bool check)
{
if (check)
VisualStateManager.GoToState(this, CheckAbleItem.RightVisualState, false);
else
VisualStateManager.GoToState(this, CheckAbleItem.FalseVisualState, false);
}
}

3、自定义CheckAbleListbox:ListBox

public class CheckAbleListbox:ListBox
{
protected override DependencyObject GetContainerForItemOverride()
{
return new CheckAbleItem();
}
}

  

4、<Style x:Key="ListBoxItemStyle1" TargetType="Helper:CheckAbleItem">

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

<Style x:Key="ListBoxItemStyle1" TargetType="Helper:CheckAbleItem">
<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="Fill" Storyboard.TargetName="bg">
<DiscreteObjectKeyFrame KeyTime="0" Value="Gray"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="text_tip">
<DiscreteObjectKeyFrame KeyTime="0" Value="Black"></DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Selected">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="bg">
<DiscreteObjectKeyFrame KeyTime="0" Value="Blue"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="text_tip">
<DiscreteObjectKeyFrame KeyTime="0" Value="White"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Right">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName ="text_tip">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value >
<Visibility >Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName ="img_right">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility >Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="bg">
<DiscreteObjectKeyFrame KeyTime="0" Value="Green"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="False">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName ="text_tip">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value >
<Visibility >Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName ="img_false">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value >
<Visibility >Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="bg">
<DiscreteObjectKeyFrame KeyTime="0" Value="Red"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Common"/> </VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid HorizontalAlignment="Left" Width="30" Height="30">
<Ellipse x:Name="bg" Fill="Red"/>
<TextBlock x:Name="text_tip" Foreground="Black" VerticalAlignment="Center" Margin="0,0,0,2" HorizontalAlignment="Center" FontSize="22" Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource converter}}"/>
<Image x:Name="img_right" Source="/Assets/Feed/right.png" Visibility="Collapsed"/>
<Image x:Name="img_false" Source="/Assets/Feed/false.png" Visibility="Collapsed"/>
</Grid> <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>

  

demo链接:http://download.csdn.net/detail/ofat___lin/7208027

自定义可判断选项是否正确listbox的更多相关文章

  1. 备份数据库的时候设置 BufferCount 选项不正确导致 out of memory 的情况

    备份数据库的时候设置 BufferCount 选项不正确导致 out of memory 的情况 今天群里面的东辉兄跟我说备份生产数据库的时候报错 环境: 32位的SQLSERVER2008 机器有1 ...

  2. idea配置tomcat运行按钮置灰,下拉没有自定义的tomcat选项

    一.问题 下拉没有自定义tomcat的选项 run按钮置灰,点不了 二.解决 添加自己的tomcat时,一定要点加号,不要用那个默认的.

  3. ArcGIS自定义工具箱-判断字段值是否相等

    ArcGIS自定义工具箱-判断字段值是否相等 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:判断两个字段值是否相等 使用方法: 结果: 联系方式:谢老师,13 ...

  4. [ASP.NET Core 3框架揭秘] Options[1]: 配置选项的正确使用方式[上篇]

    依赖注入不仅是支撑整个ASP.NET Core框架的基石,也是开发ASP.NET Core应用采用的基本编程模式,所以依赖注入十分重要.依赖注入使我们可以将依赖的功能定义成服务,最终以一种松耦合的形式 ...

  5. [ASP.NET Core 3框架揭秘] Options[2]: 配置选项的正确使用方式[下篇]

    四.直接初始化Options对象 前面演示的几个实例具有一个共同的特征,即都采用配置系统来提供绑定Options对象的原始数据,实际上,Options框架具有一个完全独立的模型,可以称为Options ...

  6. Python判断自定义的参数格式是否正确

    import argparse def args_validation(valid_list, valid_value): assert valid_value in valid_list, 'inv ...

  7. JavaScript判断是否是正确数值 isNaN

    NaN在JavaScript中表示不是数字 JavaScript中isNaN函数方法是返回一个 Boolean 值,指明提供的值是否是保留值 NaN (不是数字). 使用方法:isNaN(numVal ...

  8. Javascript中判断数组的正确姿势

    在 Javascript 中,如何判断一个变量是否是数组? 最好的方式是用 ES5 提供的 Array.isArray() 方法(毕竟原生的才是最屌的): var a = [0, 1, 2]; con ...

  9. Linux bash常用测试判断选项

    bash编程中if [   ]后面的测试选项: 1.整数测试: -le less equal -lt less than -ge greater equal -gt greater than -eq ...

随机推荐

  1. Linux 下配置网卡的别名即网卡子IP的配置 转

    what 什么是ip别名?用windows的话说,就是为一个网卡配置多个ip.when 什么场合增加ip别名能派上用场?布网需要.多ip访问测试.特定软件对多ip的需要...and so on. ho ...

  2. ios上比较好用的Cydia插件

    1.iFile查看系统文件 2.KuaiDial归属地数据库 3.KuaiDial电话拨号助手 4.搜狗输入法 Photo Editor 房贷计算器

  3. 基于Docker服务的java Web服务搭建

    导读 最近想我们的应用需要更新维护,Android.IOS.还有服务器端都要更新,都在忙于写代码没有写文章了.我们的服务器是用java ssh架构的,到时也打算切换成Spring MVC+oauth2 ...

  4. Java经典算法四十例编程详解+程序实例

    JAVA经典算法40例 [程序1]   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?   1.程 ...

  5. 解决vim中鼠标右键无法复制的问题

    转:http://www.cnblogs.com/jianyungsun/archive/2011/03/19/1988855.html 这是我的vim配置文件:jeffy-vim-v2.4.tar ...

  6. python UTC,时间戳,字符串转换

    #!/usr/bin/env python #_*_coding:utf-8_*_ # 本地时间 转换 为时间戳 import time import pytz import datetime dat ...

  7. 1.6.1 什么是 Indexing

    这部分描述了建立索引的过程:添加内容到solr索引中,如果有需要,修改内容或者删除它.通过添加内容到索引里边,我们使其内容可以搜索. solr索引能够接收不同来源的数据,xml文件,逗号分隔值的(CV ...

  8. RegisterStartupScript和RegisterClientScriptBlock的用法

    RegisterStartupScript和RegisterClientScriptBlock的用法 RegisterStartupScript(key, script) RegisterClient ...

  9. 【MongoDB】MongoDB VS SQL数据库

    MongoDB和SQL数据库都能满足数据库的基本功能:1.有组织的存放数据:2.按照需求查询数据 传统的SQL数据库(e.g.Oracle, MySQL) 对表的运用不够灵活,横向扩展不太容易,而它的 ...

  10. hdu 4424 并查集

    思路:将边从大到小排序,判断向哪边连,能使总和最大. #include<map> #include<set> #include<cmath> #include< ...