运维开发网

c# – 如何显示HeaderedItemsControl的标题?

运维开发网 https://www.qedev.com 2020-05-22 08:43 出处:网络 作者:运维开发网整理
我有以下代码: <Window.Resources> <DataTemplate x:Key="SectionTemplate" > <TextBlock Text="{Binding Path=Name}" /> </DataTemplate
我有以下代码:

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

由于某种原因,项目被渲染,但没有标题.

正如 the documentation指出:

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

因此,当您创建该模板时,请确保包含一些绑定到标题的ContentPresenter(例如,使用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>

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

0

精彩评论

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