2010-07-19 4 views
0

Actuellement, j'ai spécifié des bordures pour tous les éléments de données dans ma listbox horizontale, ce qui est correct car je veux des bordures pour tous les listbox, mais je voudrais supprimer la bordure gauche du premier élément et la bordure droite du dernier élément. Est-ce seulement possible?Suppression des côtés gauche et droit de la zone de liste datatemplate

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> 

Merci

Répondre

0

Il est, avec un DataTemplateSelector, il vous suffit de mettre en œuvre la logique pour sélectionner le DataTemplate correct, voici un exemple Fulle avec votre code. Vous devriez pouvoir copier/coller le code suivant dans votre projet. Il y a des commentaires qui expliquent ce qui se passe. J'espère que cela aide!

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; 
     } 
    } 
} 
Questions connexes