【发布时间】:2019-01-28 19:40:26
【问题描述】:
我有一个数据网格,如果单元格文本超过 32 个字符,我想将单元格设为红色。
我已经看到其他基于单元格文本何时为特定文本的解决方案,但我不确定如何在此处使用这些解决方案
我的 XAML 如下
<DataGrid Grid.Row="2" Name="DataGridView1" CanUserSortColumns="False" CanUserReorderColumns="False" IsReadOnly="True" ItemsSource="{Binding}" Background="LightGray" RowBackground="#BDBDBF" AlternatingRowBackground="#E3E3E5"></DataGrid>
为了将我的数据读入一个名为 dt 的 DataTable 中,并执行以下操作。
DataGridView1.DataContext = dt.DefaultView;
更新
使用 Daniel W 的一些代码。我已经让它部分工作,我现在只需要最后润色。
通过执行以下操作,我已使其适用于 1 列:
<DataGrid Grid.Row="2" Name="DataGridView1" CanUserSortColumns="False" CanUserReorderColumns="False" IsReadOnly="True" ItemsSource="{Binding}" Background="LightGray" RowBackground="#BDBDBF" AlternatingRowBackground="#E3E3E5">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Address1}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="{Binding Address1, Converter={StaticResource brushConverter}}">
</Setter>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
但是这确实弄乱了我的数据表的格式
https://i.gyazo.com/525ce05a30cad36458a6734d6c61a0ff.png
如您所见,我要编辑的“address1”列没有被编辑,而是在常规列的左侧创建了一个新列。
现在,我无法定义每一列,因为列名可以在运行时更改,所以我不知道我将它绑定到什么
我需要一个仅充当模板的解决方案,因为我的列在我读取 csv 之前没有定义,它可以有任意数量的列/名称,因此我无法对这些列进行数据绑定
有什么想法吗?
【问题讨论】:
标签: c# wpf colors datagrid cell