WPF DataGrid:如何将列设置为 TextWrap?
我不确定为什么我的代码没有正确执行 TextWrapping。它不会包装“描述”列的文本(这正是我想要的)。它只是将其切断,甚至没有使用“...”来让我知道还有更多数据。
我尝试使用在网上找到的代码来完成这项工作,但没有成功。理想情况下,我希望能够仅将 TextWrap 设置为某些列,而不是一般地跨所有 DataGridCell 对象。
哦,请注意,我使用的是 Microsoft.NET 4,因此这是通过它提供的 DataGrid,而不是来自 WPF 工具包。
<DataGrid Name="TestGrid" Grid.Row="2" Grid.ColumnSpan="2" AutoGenerateColumns="False" ItemsSource="{Binding IntTypes}" SelectedValue="{Binding CurrentIntType}">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Name="DataGridCellBorder">
<TextBlock Background="Transparent" TextWrapping="WrapWithOverflow" TextTrimming="CharacterEllipsis" Height="auto" Width="auto">
<ContentPresenter Content="{TemplateBinding Property=ContentControl.Content}" ContentTemplate="{TemplateBinding Property=ContentControl.Content}" />
</TextBlock>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}" IsReadOnly="True" />
<DataGridTextColumn Header="Interested Parties Description" Binding="{Binding Description}" IsReadOnly="False" />
</DataGrid.Columns>
</DataGrid>
提前致谢!
I'm not sure why my code isn't doing the TextWrapping properly. It doesn't wrap the text for the Description column (which is what I want). It just cuts it off and it doesn't even use the "..." to let me know there is more data.
I tried to use this code I found online to do the job, but it didn't work. Ideally I'd love to be able to only set TextWrap to certain columns and not generically across all DataGridCell objects.
Oh, and please do note that I am using Microsoft.NET 4 so this is the DataGrid offered through that, not from the WPF Toolkit.
<DataGrid Name="TestGrid" Grid.Row="2" Grid.ColumnSpan="2" AutoGenerateColumns="False" ItemsSource="{Binding IntTypes}" SelectedValue="{Binding CurrentIntType}">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Name="DataGridCellBorder">
<TextBlock Background="Transparent" TextWrapping="WrapWithOverflow" TextTrimming="CharacterEllipsis" Height="auto" Width="auto">
<ContentPresenter Content="{TemplateBinding Property=ContentControl.Content}" ContentTemplate="{TemplateBinding Property=ContentControl.Content}" />
</TextBlock>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}" IsReadOnly="True" />
<DataGridTextColumn Header="Interested Parties Description" Binding="{Binding Description}" IsReadOnly="False" />
</DataGrid.Columns>
</DataGrid>
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不起作用,因为 TextBlock 的“Text”属性实际上被设置为另一个对象而不仅仅是一个字符串。在运行时,您的 VisualTree 看起来像这样:
简而言之,您的代码本质上是在做类似这样的事情:
要解决此问题,请尝试按如下方式更新您的 ControlTemplate:
It Doesn't work because the "Text" property of your TextBlock is actually being set to another object instead of just a string. At runtime, your VisualTree looks something like:
In short, you're code is essentially doing something like this:
To fix this, try updating your ControlTemplate as follows: