我想在WPF应用程序中突出显示数据网格中的字符串。
在WinForms中,有一个CellPainting事件帮助我们实现这个目的。我无法在WPF中找到任何东西。
我希望高亮显示在单元格中的文本的部分,即而不是整个单元格。
任何帮助都将不胜感激。
发布于 2014-03-31 08:32:52
你可以:
添加一个DataGridTemplateColumn。在模板中放置一个TextBlock。然后,选项1:在textBlock运行中插入。设置它们的格式。并将运行结果绑定到您的数据。选项2:通过转换器等在过程代码中设置TextBlock的内容。
选项1
    <DataGridTemplateColumn>
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock>
                    <Run Text="{Binding xx}" Background="Yellow" />
                    <Run Text="{Binding yy}" />
                </TextBlock>                            
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>选项2
XAML
<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <DataTemplate.Resources>
            <myns:ConvertToFormatedRuns xmlns:myns="clr-namespace:YourProjectName" />
        </DataTemplate.Resources>
        <Label>
            <Label.Content>
                <MultiBinding Converter={StaticResource ConvertToFormatedRuns}>
                    <Binding Path="xxx" />
                    <Binding Path="yyy" />
                </MultiBinding>
            </Label.Content>
        </Label>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>码
public class ConvertToFormatedRuns : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var tb = new TextBlock();
        tb.Inlines.Add(new Run() { Text = (string)values[0], Background = Brushes.Yellow });
        tb.Inlines.Add(new Run() { Text = (string)values[1]});
        return tb;
    }
}备注:您也可以像WinForms那样绘图,但没有必要,不推荐使用。
https://stackoverflow.com/questions/22756681
复制相似问题