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

ItemContainerStyleSelector

时间:2020-05-06 01:28:22      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:templates   rri   let   ==   nim   for   eve   group   style   

原文:ItemContainerStyleSelector

 ItemContainerStyleSelector是容器Style选择器

用法和ItemTemplateSelector差不多

同样也是也是继承类 StyleSelector,也是重写方法SelectStyle,参数都是一样的,一是数据,二是数据对象。返回值则是Style,默认值则是Null

 

XAML也是差不多的,同样使用绑定的语法

 

选择器类:

技术图片
public class SelectOFStyle: StyleSelector
    {
        public int i = 0;
        public override Style SelectStyle(object item, DependencyObject container)
        {
            var u = container as FrameworkElement;

            i++;

            if (i % 2 == 0)
                return u.FindResource("St1") as Style;
            else
                return u.FindResource("St2") as  Style;
        }
    }
技术图片

 

xaml

技术图片
 <Window.Resources>
        <Storyboard x:Key="S2">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" >
                <EasingDoubleKeyFrame KeyTime="0" Value="30"/>
                <EasingDoubleKeyFrame KeyTime="0" Value="30"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="-5"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" >
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" >
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="S1">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" >
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
        <Style  TargetType="ListBoxItem" x:Key="St1">
            <Setter Property="RenderTransform" >
                <Setter.Value>
                    <TransformGroup>
                        <ScaleTransform/>
                        <SkewTransform/>
                        <RotateTransform/>
                        <TranslateTransform/>
                    </TransformGroup>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <EventTrigger  RoutedEvent="FrameworkElement.Loaded">
                    <BeginStoryboard Storyboard="{StaticResource S2}"/>
                </EventTrigger>
            </Style.Triggers>
        </Style>
        <Style  TargetType="ListBoxItem" x:Key="St2">
            <Setter Property="RenderTransform" >
                <Setter.Value>
                    <TransformGroup>
                        <ScaleTransform/>
                    </TransformGroup>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <EventTrigger  RoutedEvent="FrameworkElement.Loaded">
                    <BeginStoryboard Storyboard="{StaticResource S1  }"/>
                </EventTrigger>
            </Style.Triggers>
        </Style>
        <local:Select  x:Key="sl2"/>
        <local:SelectOFStyle x:Key="sl1"/>
        <DataTemplate x:Key="d1">
            <Image x:Name="image" Height="150"  Width="300" Source="{Binding Image}"  />
        </DataTemplate>
        
        <DataTemplate x:Key="d2">
            <Image x:Name="image" Height="100" Width="100" Source="{Binding Image}"  />
        </DataTemplate>
        
 
    </Window.Resources>
    <Grid>
        <ListBox ItemTemplateSelector="{StaticResource sl2}" ItemContainerStyleSelector="{StaticResource sl1}"  x:Name="ListBoxFile" Margin="0,0,0,119"   >
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>

        </ListBox>
        <Button Click="Button_Click" Margin="451,321,0,0"/>

    </Grid>
技术图片

 

图片效果

技术图片

ItemContainerStyleSelector

标签:templates   rri   let   ==   nim   for   eve   group   style   

原文地址:https://www.cnblogs.com/lonelyxmas/p/12833986.html

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