Windows Phone xaml Listview隐藏最后一个分隔符

时间:2015-10-09 07:45:55

标签: c# wpf xaml listview windows-phone-8.1

在我的listview中我有一个usercontrol和一个行separtor我的listview绑定在我的observable集合属性上我的xaml是:

<ListView ItemsSource="{Binding Path=ListaItinerari}" SelectedItem="{Binding ItinerarioSelezionato, Mode=TwoWay}" FontFamily="Global User Interface">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <UserControls:ItinerarioItemControl DataContext="{Binding}" />                                    
                                    <Line Margin="0,5,0,5" Stretch="Fill" Stroke="{StaticResource LineThemeBrush}" X2="1"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

我想隐藏最后一行分隔符。 我用Google搜索,但我什么都没发现

2 个答案:

答案 0 :(得分:1)

只是一个建议,但也许最好将分隔符放在最前面(因此高于你的用户控件)。

然后使用转换器对分隔符的可见性属性进行绑定。您传递项目索引,如果项目索引为1,则隐藏分隔符,否则显示它。

要获取项目的索引,请在此处查看示例:http://www.bendewey.com/index.php/523/alternating-row-color-in-windows-store-listview

答案 1 :(得分:0)

我可以使用WPF中的附加属性

来完成此操作

enter image description here

 public class IndexItemHelper
{
    #region ListIndexer

    /// <summary>
    /// ListIndexer Attached Dependency Property
    /// </summary>
    public static readonly DependencyProperty ListIndexerProperty =
        DependencyProperty.RegisterAttached("ListIndexer", typeof(List<ListItem>), typeof(IndexItemHelper),
            new FrameworkPropertyMetadata((List<ListItem>)null,
                new PropertyChangedCallback(OnListIndexerChanged)));

    /// <summary>
    /// Gets the ListIndexer property. This dependency property 
    /// indicates ....
    /// </summary>
    public static List<ListItem> GetListIndexer(DependencyObject d)
    {
        return (List<ListItem>)d.GetValue(ListIndexerProperty);
    }

    /// <summary>
    /// Sets the ListIndexer property. This dependency property 
    /// indicates ....
    /// </summary>
    public static void SetListIndexer(DependencyObject d, List<ListItem> value)
    {
        d.SetValue(ListIndexerProperty, value);
    }

    /// <summary>
    /// Handles changes to the ListIndexer property.
    /// </summary>
    private static void OnListIndexerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        List<ListItem> oldListIndexer = (List<ListItem>)e.OldValue;
        List<ListItem> newListIndexer = (List<ListItem>)d.GetValue(ListIndexerProperty);
        UpdateDependencyObject(d);
    }

    #endregion


    #region ItemIndexOf

    /// <summary>
    /// ItemIndexOf Attached Dependency Property
    /// </summary>
    public static readonly DependencyProperty ItemIndexOfProperty =
        DependencyProperty.RegisterAttached("ItemIndexOf", typeof(object), typeof(IndexItemHelper),
            new FrameworkPropertyMetadata((object)null,
                new PropertyChangedCallback(OnItemIndexOfChanged)));

    /// <summary>
    /// Gets the ItemIndexOf property. This dependency property 
    /// indicates ....
    /// </summary>
    public static object GetItemIndexOf(DependencyObject d)
    {
        return (object)d.GetValue(ItemIndexOfProperty);
    }

    /// <summary>
    /// Sets the ItemIndexOf property. This dependency property 
    /// indicates ....
    /// </summary>
    public static void SetItemIndexOf(DependencyObject d, object value)
    {
        d.SetValue(ItemIndexOfProperty, value);
    }

    /// <summary>
    /// Handles changes to the ItemIndexOf property.
    /// </summary>
    private static void OnItemIndexOfChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        object oldItemIndexOf = (object)e.OldValue;
        object newItemIndexOf = (object)d.GetValue(ItemIndexOfProperty);
        UpdateDependencyObject(d);
    }

    #endregion

    public static void UpdateDependencyObject(DependencyObject d)
    {
        var itemToGetIndexOf = GetItemIndexOf(d) as ListItem;
        var listToGetIndexFrom = GetListIndexer(d);
        if (itemToGetIndexOf == null || (listToGetIndexFrom == null || !listToGetIndexFrom.Any())) return;

        if (listToGetIndexFrom.IndexOf(itemToGetIndexOf) == (listToGetIndexFrom.Count - 1))
        {
            //this is the last line item
            var line = d as Line;
            if(line != null) line.Visibility = Visibility.Collapsed;
        }
        else
        {
            var line = d as Line;
            if (line != null) line.Visibility = Visibility.Visible;
        }
    }
}

使用附加属性:

<Grid x:Name="Main">
    <ListView ItemsSource="{Binding ListaItinerarie}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Name}"></TextBlock>
                    <Line Margin="0,5,0,5" Stretch="Fill" Stroke="Red" X2="1" stackoverflow:IndexItemHelper.ItemIndexOf="{Binding }" stackoverflow:IndexItemHelper.ListIndexer="{Binding DataContext.ListaItinerarie, ElementName=Main}"/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

每当列表或项目绑定发生变化时,这将更新......

更新:在Windows手机而不是&#39; FrameworkPropertyMetadata&#39;使用&#39; PropertyMetadata&#39;