views:

28

answers:

1

Currently I have specified borders for all datatemplated items in my horizontal listbox which is fine because I DO want borders for all individual listboxitems, but I would like to remove the left border from the first item and the right border from the last item. Is this even possible?

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>

Thanks

A: 

It is, with a DataTemplateSelector, you just need to implement the logic to select the correct DataTemplate, here's a fulle example with your code. You should be able to just copy / paste the following code in your project. There are comments in there that explain what's going on. Hope it helps!

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