wpf DataGrid更改单元格上的换行 [英] wpf DataGrid change wrapping on cells

查看:269
本文介绍了wpf DataGrid更改单元格上的换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自动生成列的WPF数据网格.我已经能够使用代码覆盖列标题,并且在收缩列时也可以强制换行.当我尝试在单元格上强制换行时,我的绑定中断了……它在每一列中显示相同的值.

I have a WPF DataGrid with AutoGenerate Columns. I have been able to override the column headers using code and also force wrapping on the column headers when I shrink the columns. When I try to force text wrapping on the cells my binding breaks... it shows the same value in every column.

这是我用来格式化的XAML

Here is the XAML I am using to format

<DataGrid.CellStyle>
   <Style TargetType="DataGridCell">
       <Setter Property="ContentTemplate">
           <Setter.Value>
               <DataTemplate>
                   <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
               </DataTemplate>
           </Setter.Value>
       </Setter>
   </Style>
</DataGrid.CellStyle>
<DataGrid.ColumnHeaderStyle>
   <Style TargetType="DataGridColumnHeader">
       <Setter Property="ContentTemplate">
           <Setter.Value>
               <DataTemplate>
                   <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
               </DataTemplate>
           </Setter.Value>
       </Setter>
   </Style>
</DataGrid.ColumnHeaderStyle>

同样,ColumnHeaderStyle可以正常工作,但是CellStyle无法正常工作.

Again, the ColumnHeaderStyle works fine, but the CellStyle is not working.

建议?

更新:

列标题设置如下:

if (e.Column.Header.ToString() == "Product_Description")
    e.Column.Header = "Product";

if (e.Column.Header.ToString() == "Original_Gross_Weight")
    e.Column.Header = "Orig. Net Wt.";

标题的换行效果很好.仅内容的包装不起作用.

The wrapping of the headers works well. Just the wrapping of the content does not work.

推荐答案

在绑定上,似乎一旦替换了DataGridCell数据样式,就将的完整对象放入内容中演示者,而不是列的当前属性.

On the binding it seems that once one replaces the DataGridCell data style, the full object for for the row is placed into the content presenter instead of the current property of the column.

您似乎覆盖了AutoGeneratingColumn,那么为什么不简单地打开自动生成并手动定义列呢?

It appears you override AutoGeneratingColumn so why not simply turn of auto generate and define the columns by hand?

这是一个工作版本,其中为数据包装了文本:

Here is a working version where the text is wrapped for the data:

<Window.Resources>
    <model:People x:Key="People">
        <model:Person First="Joe"   Last="Smith"   Phone="303-555 5555" />
        <model:Person First="Mary"  Last="Johnson" Phone="720-555 5555" />
        <model:Person First="Frank" Last="Wright"  Phone="202-555 5555" />
    </model:People>
</Window.Resources>
<DataGrid AutoGenerateColumns="False"
          ItemsSource="{StaticResource People}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="First" Binding="{Binding First}" />
        <DataGridTextColumn Header="The Name" Binding="{Binding Last}" />
        <DataGridTextColumn Header="Phone Number" Binding="{Binding Phone}">
            <DataGridTextColumn.ElementStyle>
                <Style TargetType="TextBlock">
                    <Setter Property="TextWrapping"
                            Value="Wrap" />
                </Style>
            </DataGridTextColumn.ElementStyle>

        </DataGridTextColumn>
    </DataGrid.Columns>

</DataGrid>

结果

这篇关于wpf DataGrid更改单元格上的换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆