从列表框datatemplate中删除左右边框

时间:2010-07-19 20:03:25

标签: silverlight xaml

目前我已经为水平列表框中的所有datatemplated项目指定了边框,这很好,因为我想要所有单个listboxitems的边框,但我想从第一个项目中删除左边框,从最后一个项目中删除右边框。这甚至可能吗?

的Xaml:

<ListBox.ItemTemplate>
  <DataTemplate>
    <StackPanel Orientation="Vertical" Background="DimGray">
      <Border BorderBrush="White" BorderThickness="1">
        <Canvas Height="80" Width="140">
          <TextBlock Text="{Binding Name}" TextAlignment="Center" Canvas.Top="22" Height="80" Width="140" FontSize="26"></TextBlock>
        </Canvas>
      </Border>
    </StackPanel>
  </DataTemplate>
</ListBox.ItemTemplate>

由于

1 个答案:

答案 0 :(得分:0)

使用DataTemplateSelector,您只需要实现逻辑来选择正确的DataTemplate,这里是您的代码的完整示例。您应该只能在项目中复制/粘贴以下代码。那里有评论解释发生了什么。希望它有所帮助!

XAML:

<Window x:Class="StackOverflowTests.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:local="clr-namespace:StackOverflowTests"
    Title="Window1"
    x:Name="window1"
    Width="800"
    Height="600">
    <Window.Resources>
        <!-- Instantiate the DataTemplateSelector -->
        <local:ItemDataTemplateSelector x:Key="ItemDataTemplateSelector" />
    </Window.Resources>
    <!-- Assign the DataTemplateSelector to the ListBox's ItemTemplateSelector -->
    <ListBox Margin="8" ItemsSource="{Binding}" ItemTemplateSelector="{StaticResource ItemDataTemplateSelector}">
        <ListBox.Resources>
            <!-- Template without Left border -->
            <DataTemplate x:Key="firstItemTemplate">
                <StackPanel Orientation="Vertical" Background="DimGray">
                    <Border BorderBrush="Red" BorderThickness="0,1,1,1">
                        <Canvas Height="80" Width="140">
                            <TextBlock Text="{Binding Name}" TextAlignment="Center" Canvas.Top="22" Height="80" Width="140" FontSize="26"></TextBlock>
                        </Canvas>
                    </Border>
                </StackPanel>
            </DataTemplate>
            <!-- Template with all borders -->
            <DataTemplate x:Key="regularItemTemplate">
                <StackPanel Orientation="Vertical" Background="DimGray">
                    <Border BorderBrush="Red" BorderThickness="1,1,1,1">
                        <Canvas Height="80" Width="140">
                            <TextBlock Text="{Binding Name}" TextAlignment="Center" Canvas.Top="22" Height="80" Width="140" FontSize="26"></TextBlock>
                        </Canvas>
                    </Border>
                </StackPanel>
            </DataTemplate>
            <!-- Template without the Right border -->
            <DataTemplate x:Key="lastItemTemplate">
                <StackPanel Orientation="Vertical" Background="DimGray">
                    <Border BorderBrush="Red" BorderThickness="1,1,0,1">
                        <Canvas Height="80" Width="140">
                            <TextBlock Text="{Binding Name}" TextAlignment="Center" Canvas.Top="22" Height="80" Width="140" FontSize="26"></TextBlock>
                        </Canvas>
                    </Border>
                </StackPanel>
            </DataTemplate>
        </ListBox.Resources>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
</Window>

C#:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace StackOverflowTests
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            this.DataContext = new List<Person>()
            {
                new Person() { Name = "Jim Morrison" },
                new Person() { Name = "Ozzy Osbourne" },
                new Person() { Name = "Slash" },
                new Person() { Name = "Jimmy Page" }
            };
        }
    }

    public class Person
    {
        public string Name { get; set; }
    }

    public class ItemDataTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            FrameworkElement element = container as FrameworkElement;

            // get the ListBoxItem
            ListBoxItem listBoxItem = element.TemplatedParent as ListBoxItem;

            // get the ListBoxItem's owner ListBox
            ListBox listBox = ItemsControl.ItemsControlFromItemContainer(listBoxItem) as ListBox;

            // get the index of the item in the ListBox
            int index = listBox.Items.IndexOf(item);

            // based on the index select the template
            if (index == 0)
                return element.FindResource("firstItemTemplate") as DataTemplate;
            else if (index == listBox.Items.Count - 1)
                return element.FindResource("lastItemTemplate") as DataTemplate;
            else
                return element.FindResource("regularItemTemplate") as DataTemplate;
        }
    }
}