2012-01-27 30 views
0

如果我正在为列表框设计样式,有时候我会在每个元素之间有一个分隔符。但我不希望那个分频器在最后的元素下面。带有特殊最终ListBoxItem的样式化列表框(Silverlight 5/WPF)

the problem

是否有某种形式的转换器挂羊头卖狗肉的,我可以用得到吗? (Silverlight的5)

回答得益于后下:

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:

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 
    } 
} 

注:比较需要是一个对象比较列表中的唯一字符串。否则,列表A,B,A在B之后将只有一个分隔符,因为“A”==“A”。所以这是一个使用字符串的不好的例子,但表明了一点。我试着将DataList [DataList.Count-1]转换为对象,但它似乎并没有使用传入的确切字符串。

回答

1

是的,会有一些转换器的技巧。

将分隔线的可见性绑定到列表项。然后在转换器检查返回true,如果它不是列表中的.Last()项目,并且如果它是false。您将需要访问转换器中的视图模型。

我没有现在处理的代码,但我已经做了类似于此的启用/禁用按钮。

+0

辉煌,谢谢。我会编写代码并将其放在原始帖子中。 – shane 2012-01-27 21:21:49