【问题标题】:XAML ListBox - How to change style of last item only?XAML ListBox - 如何仅更改最后一项的样式?
【发布时间】:2015-03-07 12:52:27
【问题描述】:

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

我想到了一些Visibility 绑定到x: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>

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

为了更清楚,我想在这里删除最后的蓝线:

【问题讨论】:

  • 这是Listbox的边界是吗?
  • 不,我想从列表的最后一项中隐藏x:Name="ItemSeparatorBorder"

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


【解决方案1】:

我认为最好的方法是使用 IValueConverter 来获取项目集合的引用。制作一个继承DependencyObject 的转换器,并为其赋予源列表的属性。然后就可以查看当前的item索引了:

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}}"
        ... />

【讨论】:

  • 如果我运行你的代码,Convert 方法中的Items 总是null。有没有办法使用ObservableCollection 而不是List?当使用ObservableCollection 时,ItemsCount 将等于每个项目的项目索引。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-01
  • 2021-01-05
相关资源
最近更新 更多