【问题标题】:WPF DataGrid Text wrapping in headers and elsewhereWPF DataGrid 文本在标题和其他地方换行
【发布时间】:2011-09-03 05:44:54
【问题描述】:

我想在 WPF DataGrid 列标题和行内容中启用文本换行。 在寻找解决方案时,我经常偶然发现this 之类的东西。问题是,它不适合我。

首先我对这一行有问题:

xmlns:primitives="clr-namespace:Microsoft.Windows.Controls.Primitives;assembly=WPFToolkit"

我收到有关未找到程序集的错误。 XAML 代码的其余部分存在更多问题。

<Style TargetType="{x:Type primitives:DataGridColumnHeader}">
<Setter Property="ContentTemplate">
    <Setter.Value>
        <DataTemplate>
            <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
        </DataTemplate>
    </Setter.Value>
</Setter>
</Style>

我将它放在 DataGrid 标记中,否则将无法编译。我也省略了“原始”命名空间,因为我实际上并没有包含它(见上文)。现在它编译了。但是,应用程序在窗口的构造函数中引发了一些异常。知道如何让这件事真正发挥作用吗?

【问题讨论】:

    标签: c# wpf datagrid word-wrap


    【解决方案1】:

    【讨论】:

    • 感谢您的回答。我看了看这个。由于编译错误,我必须省略 x:Key 属性。然后我只将样式标签放在 DataGrid 标签内并编译。我仍然遇到同样的崩溃。另外,我正在 c#-code 中动态创建我的列。建议的方法在这种情况下也有效吗?
    【解决方案2】:

    不需要对 app.xaml 的引用,如下所示:

    <DataGrid Name="WBdataGrid" AutoGenerateColumns="False" ColumnHeaderHeight="50" >
      <DataGrid.ColumnHeaderStyle>
        <Style TargetType="DataGridColumnHeader">
          <Setter Property="ContentTemplate">
            <Setter.Value>
              <DataTemplate>
                <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
              </DataTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      </DataGrid.ColumnHeaderStyle>
    <DataGrid.Columns>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多