应用样式的第一个孩子? [英] Apply style to first child?

查看:134
本文介绍了应用样式的第一个孩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些方法可以将样式应用于容器的第一个(或最后一个或第n个)子(任何含有子女)? 。我想自定义标签的物品的外观,使第一个具有比其他人不同的边框半径



这是我现在有:



<预类=郎咸平的XML prettyprint-覆盖> <的ControlTemplate的TargetType ={X:类型TabItem的}>
<网格和GT;
< BORDER NAME =边框BorderBrush =#666了borderThickness =1,1,1,0CornerRadius =8,8,0,0保证金=0,0,0, - 1>
< TextBlock的X:名称=TabItemText前景=#444填充=6 2TextOptions.TextFormattingMode =显示>
< ContentPresenter X:NAME =ContentSiteVerticalAlignment =中心的Horizo​​ntalAlignment =中心ContentSource =头保证金=12,2,12,2/>
< / TextBlock的>
< /边框>
< /网格和GT;
< /控件模板>


解决方案

有关的ItemsControl派生类(如TabControl的),你可以使用ItemContainerStyleSelector依赖属性。当这种依赖关系属性设置,ItemsControl的将调用StyleSelector.SelectStyle()在控制每个项目。这将允许您使用不同的项目不同的风格。



所以它的文字是大胆而比另一个大一点下面的例子改变一个TabControl最后一个选项卡项目。标签



首先,新StyleSelector类:

 类LastItemStyleSelector :StyleSelector 
{
公众覆盖风格SelectStyle(对象项目,DependencyObject的容器)
{
变种的ItemsControl = ItemsControl.ItemsControlFromItemContainer(容器);
VAR指数= itemsControl.ItemContainerGenerator.IndexFromContainer(容器);

如果(指数== itemsControl.Items.Count - 1)
{
返回(风格)itemsControl.FindResource(LastItemStyle);
}

返回base.SelectStyle(项目,容器);
}
}

这风格选择将返回风格的关键 LastItemStyle但仅限于在控制的最后一个项目。其他项目将使用默认样式。 (请注意,该功能仅使用ItemsControl的成员。它也可以用于其他ItemsControl的派生类)。接下来,在你的XAML,你首先需要创建两个资源。 。第一个资源将是这个LastItemStyleSelector,第二资源是样式

 < Window.Resources> 
<局部:LastItemStyleSelector X:键=LastItemStyleSelector/>

<风格X:键=LastItemStyle的TargetType =TabItem的>
< setter属性=粗细VALUE =大胆/>
< setter属性=字号VALUE =16/>
< /样式和GT;
< /Window.Resources>



然后最后你的TabControl的:

 < TabControl的ItemContainerStyleSelector ={StaticResource的LastItemStyleSelector}> 
< TabItem的标题=第一/>
< TabItem的标题=秒/>
< TabItem的标题=第三/>
< / TabControl的>

有关详细信息,请参阅MSDN文档:




Is there some way to apply styles to the first (or last or nth) child of a container (anything that contains children)? I am trying to customize the look of tab items so that the first one has different border radius than the others.

This is what I have now:

<ControlTemplate TargetType="{x:Type TabItem}">
    <Grid>
        <Border Name="Border" BorderBrush="#666" BorderThickness="1,1,1,0" CornerRadius="8,8,0,0" Margin="0,0,0,-1">
            <TextBlock x:Name="TabItemText" Foreground="#444" Padding="6 2" TextOptions.TextFormattingMode="Display">
                <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="12,2,12,2"/>
            </TextBlock>
        </Border>
    </Grid>
</ControlTemplate>

解决方案

For ItemsControl derived classes (such as TabControl), you can use the ItemContainerStyleSelector dependency property. When this dependency property is set, ItemsControl will call StyleSelector.SelectStyle() for each item in the control. This will allow you to use different styles for different items.

The following example changes the last tab item in a TabControl so its text is bold and a bit larger than the other tabs.

First, the new StyleSelector class:

class LastItemStyleSelector : StyleSelector
{
    public override Style SelectStyle(object item, DependencyObject container)
    {
        var itemsControl = ItemsControl.ItemsControlFromItemContainer(container);
        var index = itemsControl.ItemContainerGenerator.IndexFromContainer(container);

        if (index == itemsControl.Items.Count - 1)
        {
            return (Style)itemsControl.FindResource("LastItemStyle");
        }

        return base.SelectStyle(item, container);
    }
}

This style selector will return the style with the key "LastItemStyle" but only for the last item in the control. The other items will use the default style. (Note, that this function only uses members from ItemsControl. It could also be used for other ItemsControl derived classes.) Next, in your XAML, you first need to create two resources. The first resource will be to this LastItemStyleSelector and the second resource is the style.

<Window.Resources>
    <local:LastItemStyleSelector x:Key="LastItemStyleSelector" />

    <Style x:Key="LastItemStyle" TargetType="TabItem">
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="FontSize" Value="16" />
    </Style>
</Window.Resources>

Then finally your TabControl:

    <TabControl ItemContainerStyleSelector="{StaticResource LastItemStyleSelector}">
        <TabItem Header="First" />
        <TabItem Header="Second" />
        <TabItem Header="Third" />
    </TabControl>

For more information see the MSDN documentation:

这篇关于应用样式的第一个孩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆