码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-04-23 01:50:42      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

原文 获取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 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、<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

标签:

原文地址:http://www.cnblogs.com/lonelyxmas/p/4449045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!