XAML ListBox - 如何仅更改最后一项的样式?

时间:2015-01-09 08:34:03

标签: c# xaml windows-phone-8 .net-4.0 listbox

我有RadDataBoundListBox(来自Telerik)代表List的项目。每个项目由底线x:Name="ItemSeparatorBorder"分隔。 ListBox本身也有一个页眉和一个包含一行的页脚(x:Name="ListTopBorder"x:Name="ListBottomBorder")。现在我需要一种方法禁用此ListBox中最后一项的行x:Name="ItemSeparatorBorder")。

我想到了一些Visibilityx:Name="ItemSeparatorBorder"的绑定,其Converter与当前项的索引与ListBox的总计数相匹配。但我不知道如何实现它,我找不到任何好的样本。

代码应该适用于Windows Phone 8.0 / .NET 4.0。

到目前为止,这是我的代码:

    <telerikPrimitives:RadDataBoundListBox
        x:Name="ListBox"
        ItemsSource="{Binding Items}">

        <telerikPrimitives:RadDataBoundListBox.ListHeaderTemplate>
            <DataTemplate>
                <Grid Height="30">
                    <Border
                        x:Name="ListTopBorder"
                        Height="1" 
                        VerticalAlignment="Bottom"
                        Background="Blue"/>
                </Grid>
            </DataTemplate>
        </telerikPrimitives:RadDataBoundListBox.ListHeaderTemplate>

        <telerikPrimitives:RadDataBoundListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="1"></RowDefinition>
                    </Grid.RowDefinitions>

                    <controls:ListItem Margin="30,10,0,10">/>

                    <Border 
                        x:Name="ItemSeparatorBorder"
                        Grid.Row="1" 
                        Height="1" 
                        Background="Blue" 
                        Margin="30,0,0,0"/>

                </Grid>
            </DataTemplate>
        </telerikPrimitives:RadDataBoundListBox.ItemTemplate>

        <telerikPrimitives:RadDataBoundListBox.ListFooterTemplate>
            <DataTemplate>
                <Grid Height="30">
                    <Border
                        x:Name="ListBottomBorder"
                        Height="1" 
                        VerticalAlignment="Top"
                        Background="Blue"/>
                </Grid>
            </DataTemplate>
        </telerikPrimitives:RadDataBoundListBox.ListFooterTemplate>

    </telerikPrimitives:RadDataBoundListBox>

如何隐藏最后一项的边框?

为了更清楚,我想删除最后的蓝线: enter image description here

1 个答案:

答案 0 :(得分:0)

我认为最好的方法是使用IValueConverter来获取对items集合的引用。创建一个继承DependencyObject的转换器,并为其提供源列表的属性。然后你可以检查当前的项目索引:

public class ItemToVisibilityConverter : DependencyObject, IValueConverter
{
    public IList Items
    {
        get { return (IList )GetValue(ItemsProperty); }
        set { SetValue(ItemsProperty, value); }
    }
    public static readonly DependencyProperty ItemsProperty=
        DependencyProperty.Register("Items", typeof(IList), typeof(ItemToVisibilityConverter), new PropertyMetadata(null));

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool hide = Items != null 
            && value != null 
            && Items.IndexOf(value) == Items.Count - 1;
        return (hide ? Visibility.Collapsed : Visibility.Visible);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException;
    }
}

将转换器实例放入控件的资源中:

<telerikPrimitives:RadDataBoundListBox
        x:Name="ListBox"
        ItemsSource="{Binding Items}">
    <telerikPrimitives:RadDataBoundListBox.Resources>
        <ResourceDictionary>
            <local:ItemToVisibilityConverter x:Key="ItemToVisibilityConverter"
                    Items="{Binding Items}" />
        </ResourceDictionary>
    </telerikPrimitives:RadDataBoundListBox.Resources>

    ...
</telerikPrimitives:RadDataBoundListBox>

最后,在“ItemTemplate”中,使用转换器绑定到当前项:

<Border x:Name="ItemSeparatorBorder"
        Visibility="{Binding Converter={StaticResource ItemToVisibilityConverter}}"
        ... />