2011-08-24 64 views
1

我使用列表框分組分組,我想左頭部和子元件之間,避免垂直空間,如下所示:避免空間,同時用列表框

enter image description here

下面是相同

的XAML
<Grid> 
    <Grid.Resources> 
     <XmlDataProvider x:Key="Company" XPath="/Company"> 
      <x:XData> 
       <Company xmlns=""> 
        <Person Name="Jack" Role="CEO"/> 
        <Person Name="Tim" Role="PL" /> 
        <Person Name="Jil" Role="PL" /> 
        <Person Name="Jimmy" Role="PM" /> 
        <Person Name="Joy" Role="PM" /> 
        <Person Name="Jim" Role="PL" /> 
        <Person Name="Jack" Role="PM" /> 
       </Company> 
      </x:XData> 
     </XmlDataProvider> 

     <DataTemplate x:Key="categoryTemplate"> 
      <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Background="ForestGreen" Margin="0,5,0,0"/> 
     </DataTemplate> 
     <DataTemplate x:Key="template"> 
      <TextBlock Text="{Binding [email protected]}"/> 
     </DataTemplate> 

     <CollectionViewSource x:Key="cvs" Source="{Binding Source={StaticResource Company},XPath=Person}"> 
      <CollectionViewSource.GroupDescriptions> 
       <PropertyGroupDescription PropertyName="@Role"/> 
      </CollectionViewSource.GroupDescriptions> 
     </CollectionViewSource> 
    </Grid.Resources> 


    <ListBox Name="lst" ItemTemplate="{StaticResource template}" ItemsSource="{Binding Source={StaticResource cvs}}"> 
     <ListBox.GroupStyle> 
      <GroupStyle HeaderTemplate="{StaticResource categoryTemplate}" /> 
     </ListBox.GroupStyle> 
    </ListBox> 
</Grid> 
+0

你試過用網格的大小來縮小網格的寬度嗎? – Kevin

回答

3

ListBox將使用GroupItems當你組,他們有一個默認Margin設置爲5,0,0,0。此外,ListBoxItem附帶默認Padding2,0,0,0。你可以改變它們中的任何一個,或者兩者都是這樣的

<ListBox ...> 
    <ListBox.Resources> 
     <Style TargetType="{x:Type GroupItem}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type GroupItem}"> 
         <StackPanel> 
          <ContentPresenter/> 
          <ItemsPresenter Margin="0,0,0,0"/> 
         </StackPanel> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </ListBox.Resources> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <Setter Property="Padding" Value="0"/> 
     </Style> 
    </ListBox.ItemContainerStyle> 
    <!--...--> 
</ListBox> 
0

檢查這是否適合您。剛剛修改你的模板位。

<DataTemplate x:Key="template"> 
    <TextBlock Text="{Binding [email protected]}" Margin="-5,0,0,0"/> 
</DataTemplate>