如何在WPF的DataGrid单元格中修改部分字符串的前景色及样式
WPF DataGrid单元格部分文本样式修改
现有代码
MainWindow.xaml
<Window x:Class="Helloone.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Helloone" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <DataGrid x:Name="DG1" Margin="0,100,0,0" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="text1" Binding="{Binding Onedata}" Width="150"/> <DataGridTextColumn Header="text2" Binding="{Binding Twodata}" Width="150"/> </DataGrid.Columns> </DataGrid> </Grid> </Window>
MainWindow.xaml.cs
namespace Helloone { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); List<MyCell> listaaa = new List<MyCell>(); listaaa.Add(new MyCell() { Onedata="abc", Twodata ="def"}); listaaa.Add(new MyCell() { Onedata="ghj", Twodata ="yougood"}); DG1.ItemsSource= listaaa; } } public class MyCell { public string? Onedata { get; set; } public string? Twodata { get; set; } } }
当前效果
DataGrid内所有单元格均为普通文本样式,无部分文本的特殊格式。
需求
修改单元格内部分字符串样式:例如第二行第二列的yougood,将其中的good设置为红色加粗,you保持原有样式不变。
解决方案
方法一:使用绑定转换器处理文本样式
- 新建转换器类,将字符串转换为带样式的Inline集合:
using System.Globalization; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Media; namespace Helloone { public class TextPartStyleConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is string text && !string.IsNullOrEmpty(text)) { var inlines = new InlineCollection(); // 针对yougood做拆分,可根据实际需求调整匹配逻辑 if (text.Equals("yougood")) { inlines.Add(new Run("you")); var boldRedRun = new Run("good") { Foreground = Brushes.Red, FontWeight = FontWeights.Bold }; inlines.Add(boldRedRun); } else { inlines.Add(new Run(text)); } return inlines; } return null; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }
- 修改XAML,替换列类型并使用转换器:
<Window x:Class="Helloone.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Helloone" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <local:TextPartStyleConverter x:Key="TextPartStyleConverter"/> </Window.Resources> <Grid> <DataGrid x:Name="DG1" Margin="0,100,0,0" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="text1" Binding="{Binding Onedata}" Width="150"/> <DataGridTemplateColumn Header="text2" Width="150"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Inlines="{Binding Twodata, Converter={StaticResource TextPartStyleConverter}}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Grid> </Window>
方法二:在数据模型中直接提供格式化内容
如果需要更灵活的样式控制,可在数据类中添加返回格式化内容的属性:
namespace Helloone { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); List<MyCell> listaaa = new List<MyCell>(); listaaa.Add(new MyCell() { Onedata="abc", Twodata ="def"}); listaaa.Add(new MyCell() { Onedata="ghj", Twodata ="yougood"}); DG1.ItemsSource= listaaa; } } public class MyCell { public string? Onedata { get; set; } public string? Twodata { get; set; } public InlineCollection FormattedTwodata { get { var inlines = new InlineCollection(); if (Twodata == "yougood") { inlines.Add(new Run("you")); inlines.Add(new Run("good") { Foreground = Brushes.Red, FontWeight = FontWeights.Bold }); } else { inlines.Add(new Run(Twodata ?? string.Empty)); } return inlines; } } } }
然后修改XAML中的列:
<DataGridTemplateColumn Header="text2" Width="150"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Inlines="{Binding FormattedTwodata}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>
内容的提问来源于stack exchange,提问作者CsharpIsGood
相关产品推荐
相关产品推荐

