【问题标题】:GridView does not display headersGridView 不显示标题
【发布时间】:2012-09-29 19:14:30
【问题描述】:

我有一个ListView,其中GridView.ColumnHeaderTemplate 是:

<DataTemplate x:Key="HeaderTemplate">
    <TextBlock />
</DataTemplate>

但是当我添加新列时,标题不显示任何文本:

GridViewColumn column = new GridViewColumn();
column.Header = new TextBlock { Text = "my header" };
MyGridView.Columns.Add(column);

为什么?我该如何解决这个问题?

【问题讨论】:

    标签: c# wpf templates listview gridview


    【解决方案1】:

    是否忘记添加 Text 属性绑定?

    <DataTemplate x:Key="HeaderTemplate">
        <TextBlock Text="{Binding}" />
    </DataTemplate>
    

    【讨论】:

      【解决方案2】:

      为什么需要显式声明ColumnHeaderTemplate,因为默认情况下它是textBlock?只需摆脱模板并将您的代码隐藏更改为此 -

      GridViewColumn column = new GridViewColumn();
      column.Header = "my header";
      MyGridView.Columns.Add(column);
      

      只需set your string to the Header 属性。

      编辑

      如果您想使用背景和字体样式,您需要保留 textBlock。因此,按照上面的建议保留代码,即只需使用字符串设置标题并在您的 xaml 绑定文本属性中 -

      <DataTemplate x:Key="HeaderTemplate">
          <TextBlock Text="{Binding}" Background="Red" />
      </DataTemplate>
      

      【讨论】:

      • 这样你的 column.Header 是一个字符串,而不是一个 TextBlock。而且我无法更改背景或字体家族(例如)。
      • 更新了答案。看看有没有帮助.. :)
      猜你喜欢
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 2010-09-26
      相关资源
      最近更新 更多