如何显示HeaderedItemsControl的标题?

jer*_*rys 5 c# wpf xaml headereditemscontrol

我有以下代码:

 <Window.Resources>      
       <DataTemplate x:Key="SectionTemplate" >                          
              <TextBlock Text="{Binding Path=Name}" />                  
       </DataTemplate>
 </Window.Resources>
 <Grid >        
   <Border>
       <HeaderedItemsControl Header="Top1"
                             ItemsSource="{Binding Path=List1}" 
                             ItemTemplate="{StaticResource SectionTemplate}"/>
    </Border>       
 </Grid>
Run Code Online (Sandbox Code Playgroud)
public class MainWindow
{
   public List<Item> List1
   {
      get { return list1; }
      set { list1 = value; }
   }

   public MainWindow()
   {             
      list1.Add(new Item { Name = "abc" });
      list1.Add(new Item { Name = "xxx" });

      this.DataContext = this;      
      InitializeComponent();       
   }   
}

public class Item
{     
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

由于某种原因,Items渲染,但没有标题.

H.B*_*.B. 7

正如文档指出:

HeaderedItemsControl具有有限的默认样式.要使用自定义外观创建HeaderedItemsControl,请创建一个新的ControlTemplate.

因此,当您创建该模板时,请确保包含一些ContentPresenter绑定到的模板Header(例如使用ContentSource)

例如

<HeaderedItemsControl.Template>
    <ControlTemplate TargetType="{x:Type HeaderedItemsControl}">
        <Border>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <ContentPresenter ContentSource="Header" />
                <Separator Grid.Row="1" />
                <ItemsPresenter Grid.Row="2" />
            </Grid>
        </Border>                       
    </ControlTemplate>
</HeaderedItemsControl.Template>
Run Code Online (Sandbox Code Playgroud)

(省略所有默认绑定(边距,背景等).)

  • 这确实是一种有限的默认样式 - 在我看来,默认样式至少应该以非常基本的方式显示标题。 (2认同)