2010-07-19 113 views
0

目前,我已經爲我的水平列表框中的所有數據特徵項目指定了邊界,這很好,因爲我希望爲所有單個列表框項目指定邊框,但是我想從第一個邊框中刪除左邊框項目和最後一個項目的右邊框。這甚至有可能嗎?從列表框數據模板中刪除左側和右側邊框

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

感謝

回答

0

它是一個DataTemplateSelector,你只需要實現選擇正確的DataTemplate的邏輯,這裏是你的代碼fulle例子。您應該能夠將以下代碼複製/粘貼到您的項目中。那裏有評論,解釋發生了什麼事。希望能幫助到你!

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