[英]How to make a row bold in WPF DataGrid
I have a DataGrid containing four rows, and I would need to make the texts in the last row bold, in order to better separate them from the rows above. 我有一个包含四行的DataGrid,并且我需要使最后一行中的文本加粗,以更好地将它们与上面的行分开。
I tried the methods available in the question How to change a single datagrid row FontWeights to Bold? 我尝试了如何将单个数据网格行FontWeights更改为粗体时可用的方法? , but I was unable to get it working. ,但我无法使其正常运行。
This is the code that I tried; 这是我尝试过的代码; running it results in an error, as row
is null. 运行它会导致错误,因为row
为null。
Setter bold = new Setter(TextBlock.FontWeightProperty, FontWeights.Bold, null);
DataGridRow row = (DataGridRow)DG_PPC.ItemContainerGenerator.ContainerFromIndex(3);
Style newStyle = new Style(row.GetType());
newStyle.Setters.Add(bold);
row.Style = newStyle;
I would appreciate any help you can give me. 您能给我任何帮助,我将不胜感激。 Thank you! 谢谢!
XAML code: XAML代码:
<DataGrid x:Name="DG_PPC" HorizontalAlignment="Left" Height="115" Margin="661,-6,0,0"
HeadersVisibility="Column" VerticalAlignment="Top" Width="726.25"
Loaded="DataGrid_PPC_Loaded" RowHeaderWidth="0" AutoGenerateColumns="False"
CanUserSortColumns="False" CanUserReorderColumns="False" FontSize="12" IsReadOnly="True">
I found myself another way to do it, which is compatible with my code. 我发现自己有另一种方式来做到这一点,与我的代码兼容。 Here's the solution, in case someone will be needing something similar. 如果有人需要类似的东西,这就是解决方案。
APP.XML: app.xml中:
<Application.Resources>
<local:FontWeightConverter x:Key="FontWeightConverter"/>
</Application.Resources>
XAML: XAML:
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="FontWeight" Value="{Binding RelativeSource={RelativeSource Self},
Path=Item.XYZ, Converter={StaticResource FontWeightConverter}}"/>
</Style>
</DataGrid.RowStyle>
Code: 码:
class FontWeightConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string name = (string)value;
if (name.Equals("Δ"))
return FontWeights.Bold;
else
return FontWeights.Normal;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.