【问题标题】:How to display HeaderedItemsControl's Header?如何显示 HeaderedItemsControl 的 Header?
【发布时间】:2011-08-28 12:36:11
【问题描述】:

我有以下代码:

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

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

【问题讨论】:

    标签: c# wpf xaml headereditemscontrol


    【解决方案1】:

    正如the documentation 指出的那样:

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

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

    所有默认绑定(边距、背景等)都被省略了。

    【讨论】:

    • 在 XAML 中的什么位置?
    • @Doug:阅读this
    • 这确实是一种有限的默认样式——在我看来,默认样式至少应该以非常基本的方式显示标题。
    【解决方案2】:

    您可以为标题创建DataTemplate,就像为项目所做的那样(这肯定比重做整个控件模板的侵入性要小)。

    例如

    <Window.Resources>
      <DataTemplate x:Key="HeaderTemplate">
      </DataTemplate>
    </Window.Resources>
    
    <HeaderedItemsControl Header="Top1"
                          HeaderTemplate="{StaticResource HeaderTemplate}"
                          ItemsSource="{Binding Path=List1}"
                          ItemTemplate="{StaticResource SectionTemplate}"
                          />
    

    而不是使用例如此处的“Top1”,您可以绑定到一个对象,然后在 DataTemplate 中使用相对于该对象的绑定。

    这种方法有一个问题,那就是为非控制元素(尤其是TextBlock)引入样式的必要方法有点复杂;另见WPF Some styles not applied on DataTemplate controls。 (您也可能在使用 ControlTemplate 方法时遇到这种情况。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-14
      • 2023-03-19
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 2015-01-05
      • 2016-12-07
      • 1970-01-01
      相关资源
      最近更新 更多