开发者

How to display HeaderedItemsControl's Header?

I have the following code:

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

For some reason the Items are rendered, but without the header.


As the documentation points out:

A HeaderedItemsControl has a limited default style. To create a HeaderedItemsControl with a custom appearance, create a new ControlTemplate.

So when you create that template make sure to include some ContentPresenter which is bound to the Header (e.g. using ContentSource)

e.g.

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

(All the default bindings (Margin, Background, etc.) are omitted.)


You can make a DataTemplate for the header, just as you did for the items (which is surely less intrusive than redoing the entire control template).

e.g.

<Window.Resources>
  <DataTemplate x:Key="HeaderTemplate">
  </DataTemplate>
</Window.Resources>

<HeaderedItemsControl Header="Top1"
                      HeaderTemplate="{StaticResource HeaderTemplate}"
                      ItemsSource="{Binding Path=List1}"
                      ItemTemplate="{StaticResource SectionTemplate}"
                      />

Instead of using e.g. "Top1" as here, you can bind to an object and then use binding relative to that object in the DataTemplate.

There is one gotcha with this approach, which is that the necessary approach to getting styles pulled in for non-control elements (notably TextBlock) is a little convoluted; also see WPF Some styles not applied on DataTemplate controls. (You might also run into this with the ControlTemplate approach.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜