截图如下:

      

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. 不相交集(The Disjoint Set ADT)

    0)引论 不相交集是解决等价问题的一种有效的数据结构,之所以称之为有效是因为,这个数据结构简单(几行代码,一个简单数组就可以搞定),快速(每个操作基本上可以在常数平均时间内搞定). 首先我们要明白什么 ...

  2. IDEA加密(转)

    1. 简介 IDEA是International Data Encryption Algorithm 的缩写,是1990年由瑞士联邦技术学院来学嘉X.J.Lai 和Massey提出的建议标准算法称作P ...

  3. qregularexpression和qregexp的区别

    QRegularExpression 是Qt 5.0才引进的,相对于QRegExp,QRegularExpression class修复了很多bug,提高了效率,提供了对Perl的RegEx几乎全面兼 ...

  4. Java再学习——CopyOnWrite容器

    一,定义 CopyOnWrite容器即写时复制的容器.通俗的理解是当我们往一个容器添加元素的时候,不直接往当前容器添加,而是先将当前容器进行Copy,复制出一个新的容器,然后新的容器里添加元素,添加完 ...

  5. Seedwork

    Seedwork      源框架 Martin Fowler               马丁·福勒 11 September 2003      2003-9-11 In the very ear ...

  6. Qt 静态编译的问题.

    编译参数 configure -confirm-license -opensource -developer-build -static -prefix D:\libraries\Qt5.3.1s - ...

  7. LeetCode24 Swap Nodes in Pairs

    题意: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...

  8. BootStrap2学习日记15----选项卡

    导航格式1: <ul class="nav nav-tabs"> <li class="active"><a href=" ...

  9. react-redux 学习笔记

    react 是 view 层的一个框架,负责展示数据:redux 控制数据流动,把数据存在唯一的 store 里,通过 action 来触发事件,reducer 来根据事件处理数据. redux 在通 ...

  10. JavaScript break和continue 跳出循环

    在JavaScript中,使用 break 和 continue 语句跳出循环: break语句的作用是立即跳出循环,即不再执行后面的所有循环: continue语句的作用是停止正在执行的循环,直接进 ...