【问题标题】:Windows Phone xaml Listview hide last separatorWindows Phone xaml Listview 隐藏最后一个分隔符
【发布时间】:2015-10-09 07:45:54
【问题描述】:

在我的列表视图中,我有一个用户控件和一个行分隔符,我的列表视图绑定在我的可观察集合属性上,我的 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>

我想隐藏最后一行分隔符。 我用谷歌搜索了,但我什么也没找到

【问题讨论】:

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


    【解决方案1】:

    只是一个建议,但最好将分隔符放在顶部(因此在您的用户控件上方)。

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

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

    【讨论】:

    • 你不明白我的回答...把分隔符放在上面就不再是最后一个了!但还会有第一次。但这更容易被发现和隐藏。
    • 是的,我现在明白了.. 这是个好主意,但是他所要求的也可以使用附加属性来实现..
    【解决方案2】:

    我已经能够使用 WPF 中的附加属性来做到这一点

     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 phone 中使用“PropertyMetadata”代替“FrameworkPropertyMetadata”

    【讨论】:

    • 抱歉,Windows phone 8.1 xaml 中的 FrameworkPropertyMetadata 类型未找到
    • 改用 PropertyMetadata!
    猜你喜欢
    • 2013-08-07
    • 2012-12-21
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多