简体   繁体   English

使用特殊的最终ListBoxItem(Silverlight 5 / WPF)对列表框进行样式设置

[英]Styling Listboxes with special final ListBoxItem (Silverlight 5/WPF)

If I am styling a list box, sometimes I'll have a divider between each element. 如果我在设计列表框的样式,有时在每个元素之间都会有一个分隔符。 BUT I don't want that divider below the final element. 但是我不希望最后一个元素下面的分隔线。

问题

Is there some sort of converter trickery I can use to get this? 我可以使用某种转换器技巧来获得此功能吗? (Silverlight 5) (Silverlight 5)

Answer thanks to the below post: 感谢以下帖子:

XAML: XAML:

<UserControl
    x:Class="SilverlightApplication41.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SilverlightApplication41"
    Width="640" Height="480">

    <UserControl.Resources>
        <local:NotLastItemToVisibilityConverter x:Key="NotLastItemToVisibilityConverter"/>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">
        <Border Background="#EEEDED" HorizontalAlignment="Left" VerticalAlignment="Center">
            <ListBox x:Name="_listbox" Background="#FFEEEDED" BorderBrush="#FF585858">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid Margin="2">
                            <TextBlock Text="{Binding}"/>
                            <Rectangle Fill="Orange" Height="2" VerticalAlignment="Bottom" Margin="1,0,1,-2"
                                       Visibility="{Binding Converter={StaticResource NotLastItemToVisibilityConverter}}"/>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Border>
    </Grid>
</UserControl>

CS: CS:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Collections.Generic;

namespace SilverlightApplication41
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            // Required to initialize variables
            InitializeComponent();
            List<string> dataList = new List<string>(){"Ants", "Bats", "Cats", "Dogs", "Entoloma sinuatum"};
            ((NotLastItemToVisibilityConverter)Resources["NotLastItemToVisibilityConverter"]).DataList = dataList;
            _listbox.ItemsSource = dataList;
        }

    }

    public class NotLastItemToVisibilityConverter : IValueConverter
    {
        public List<string> DataList {get; set;}

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if(DataList.Count == 0 || (string)value == DataList[DataList.Count-1])
            {
                return Visibility.Collapsed;
            }
            return Visibility.Visible;
        }

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

        #endregion
    }
}

Note: The comparison needs to be an object comparison OR unique strings in the list. 注意:比较必须是对象比较或列表中的唯一字符串。 Otherwise the list A,B,A would only have a divider after B since "A" == "A". 否则,由于“ A” ==“ A”,列表A,B,A仅在B之后有一个分隔符。 So this is a bad example using a string but makes the point. 因此,这是一个使用字符串的错误示例,但很重要。 I tried casting DataList[DataList.Count-1] to an object but it doesn't seem that the binding uses the exact string passed in. 我尝试将DataList [DataList.Count-1]强制转换为对象,但是绑定似乎并没有使用传入的确切字符串。

Yes there will be some converter trickery. 是的,会有一些转换器欺骗。

Bind the visibility of the divider to the list item. 将分隔线的可见性绑定到列表项。 Then in the converter check return true if it's not the .Last() item in the list and false if it is. 然后,在转换器中,如果不是列表中的.Last()项,则返回true否则返回false You will need to get access to the view model in the converter. 您将需要访问转换器中的视图模型。

I don't have the code to hand right now, but I've done something similar to this to enable/disable buttons. 我现在没有手头的代码,但是我已经做了类似的事情来启用/禁用按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM