WPF Datagrid 单元格文本换行
WPF Datagrid Cell Text Wrapping
我有一个具有自定义样式的数据网格,因此我可以在整个应用程序中重复使用这种格式。它具有自定义列 header 样式、行样式等。我能够让文本换行在列 header 上工作并且数据正确绑定到它。当我尝试在单元格上使用相同的技术时,绑定似乎不起作用,但包裹却起作用了。我已阅读以下帖子,但看起来我每次放置数据网格后都需要设置样式。它不能在资源字典中完成,还是我在错误的地方应用了包装?
WPF toolkit datagrid cell text wrapping
WPF DataGrid Cell Text Wrapping - set to NoWrap (False)
这是数据网格定义(已修剪):
<Style x:Key="EmbedDG" TargetType="{x:Type DataGrid}" >
<Setter Property="ColumnHeaderStyle" Value="{DynamicResource DGCH}" />
<Setter Property="CellStyle" Value="{DynamicResource EmbedDGCE}" />
</Style>
这是显示文本换行的工作 DGCH 样式:
<Style x:Key="DGCH" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
这里是不起作用的cellstyle(图1有contenttemplate,2没有):
<Style x:Key="EmbedDGCE" TargetType="{x:Type DataGridCell}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
编辑:
<DataGrid Style="{DynamicResource EmbedDG}" ItemsSource="{Binding Tags}" >
<DataGrid.Columns>
<DataGridTextColumn Header="Tag Type" Binding="{Binding TagType}" Width="180" />
<DataGridTextColumn Header="Tag Comments" Binding="{Binding Message}" Width="300"/>
</DataGrid.Columns>
</DataGrid>
我会摆脱单元格样式并使用模板列。
<DataGrid Style="{DynamicResource EmbedDG}" ItemsSource="{Binding Tags}" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Tag Type" Width="180">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Text="{Binding TagType}"
TextWrapping="Wrap"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Tag Comments" Width="300">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Text="{Binding Message}"
TextWrapping="Wrap"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我想它会为您提供完全限定的 class 名称,因为您的单元格模板正试图在 TextBlock 中显示一个对象。我没有时间玩它,但无论您的代码中有什么问题,以上内容都应该有效。
我有一个具有自定义样式的数据网格,因此我可以在整个应用程序中重复使用这种格式。它具有自定义列 header 样式、行样式等。我能够让文本换行在列 header 上工作并且数据正确绑定到它。当我尝试在单元格上使用相同的技术时,绑定似乎不起作用,但包裹却起作用了。我已阅读以下帖子,但看起来我每次放置数据网格后都需要设置样式。它不能在资源字典中完成,还是我在错误的地方应用了包装?
WPF toolkit datagrid cell text wrapping
WPF DataGrid Cell Text Wrapping - set to NoWrap (False)
这是数据网格定义(已修剪):
<Style x:Key="EmbedDG" TargetType="{x:Type DataGrid}" >
<Setter Property="ColumnHeaderStyle" Value="{DynamicResource DGCH}" />
<Setter Property="CellStyle" Value="{DynamicResource EmbedDGCE}" />
</Style>
这是显示文本换行的工作 DGCH 样式:
<Style x:Key="DGCH" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
这里是不起作用的cellstyle(图1有contenttemplate,2没有):
<Style x:Key="EmbedDGCE" TargetType="{x:Type DataGridCell}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
编辑:
<DataGrid Style="{DynamicResource EmbedDG}" ItemsSource="{Binding Tags}" >
<DataGrid.Columns>
<DataGridTextColumn Header="Tag Type" Binding="{Binding TagType}" Width="180" />
<DataGridTextColumn Header="Tag Comments" Binding="{Binding Message}" Width="300"/>
</DataGrid.Columns>
</DataGrid>
我会摆脱单元格样式并使用模板列。
<DataGrid Style="{DynamicResource EmbedDG}" ItemsSource="{Binding Tags}" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Tag Type" Width="180">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Text="{Binding TagType}"
TextWrapping="Wrap"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Tag Comments" Width="300">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Text="{Binding Message}"
TextWrapping="Wrap"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我想它会为您提供完全限定的 class 名称,因为您的单元格模板正试图在 TextBlock 中显示一个对象。我没有时间玩它,但无论您的代码中有什么问题,以上内容都应该有效。