加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 01:46:56 所属栏目:百科 来源:网络整理
导读:我有以下代码: Window.Resources DataTemplate x:Key="SectionTemplate" TextBlock Text="{Binding Path=Name}" / /DataTemplate /Window.Resources Grid Border HeaderedItemsControl Header="Top1" ItemsSource="{Binding Path=List1}" ItemTemplate="{St
我有以下代码:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读