[C#7.0 - WPF] - RichTextBox in a DataGridTemplateColumn with binding

BlackSun 21 Reputation points
2020-04-21T17:10:59.81+00:00

Hi,
I'm getting crazy about a RichTextBox to insert and bind to a DataGridTemplateColumn.

I have 7478-immagine-2.jpg

As you can see, in a cell of the DataGridTemplateColumn I have four lines and what I would like is to have these lines colored in different way (one color for esch line).

This is how I generate the column:

      <DataGridTemplateColumn Header="LOCATION" Width="*" CanUserSort="False" CanUserReorder="False" IsReadOnly="True">  
                    <DataGridTemplateColumn.CellTemplate>  
                        <DataTemplate>  
                            <RichTextBox IsEnabled="False" BorderThickness="0" Background="Transparent">  
                                <FlowDocument>  
                                    <Paragraph>  
                                        <Run Text="{Binding Location}"/>  
                                    </Paragraph>  
                                </FlowDocument>  
                            </RichTextBox>  
                        </DataTemplate>  
                    </DataGridTemplateColumn.CellTemplate>  
                </DataGridTemplateColumn>  

What have I to do??

Thanks in advance,
BlackSun

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,667 questions
0 comments No comments
{count} votes

Accepted answer
  1. Alex Li-MSFT 1,096 Reputation points
    2020-04-22T09:40:07.787+00:00

    fix my code:

     vm.People =  new List<MyClass>(){ new MyClass (){  
                    Location =new List<string> (){ "IP:","10","S/N:","6725542","Tag:","602","Mod:","Phaser"},  
                    solidColorBrush =new List<SolidColorBrush> (){ new SolidColorBrush (Colors.Black),new SolidColorBrush (Colors.Red),new SolidColorBrush(Colors.Aqua),new SolidColorBrush (Colors.Aquamarine), new SolidColorBrush(Colors.Silver), new SolidColorBrush(Colors.Turquoise) } },            
                };  
    

    xaml:

     <StackPanel >  
                                    <StackPanel Orientation="Horizontal">  
                                        <TextBlock Text="{Binding Location[0]}" Foreground="{Binding solidColorBrush[0]}" FontWeight="Bold"/>  
                                        <TextBlock Text="{Binding Location[1]}" Foreground="{Binding solidColorBrush[1]}" />  
                                    </StackPanel>  
                                    <StackPanel Orientation="Horizontal">  
                                        <TextBlock Text="{Binding Location[2]}" Foreground="{Binding solidColorBrush[2]}" FontWeight="Bold"/>  
                                        <TextBlock Text="{Binding Location[3]}" Foreground="{Binding solidColorBrush[3]}" />  
                                    </StackPanel>  
                                    <StackPanel Orientation="Horizontal">  
                                        <TextBlock Text="{Binding Location[4]}" Foreground="{Binding solidColorBrush[4]}" FontWeight="Bold"/>  
                                        <TextBlock Text="{Binding Location[5]}" Foreground="{Binding solidColorBrush[5]}" />  
                                    </StackPanel>  
                                </StackPanel>  
    

    7594-annotation-2020-04-22-173832.png


2 additional answers

Sort by: Most helpful
  1. Lloyd Sheen 1,351 Reputation points
    2020-04-21T21:33:06.53+00:00

    Both Run and Paragraph have a BackGround property which you can bind to, to get different colours for each element.


  2. Alex Li-MSFT 1,096 Reputation points
    2020-04-22T04:35:10.143+00:00

    Welcome to our Microsoft Q&A platform!

    I think you can use Stackpanel:

    xaml:

     <DataGridTemplateColumn  Header="LOCATION" Width="*"  CanUserSort="True" CanUserReorder="False" IsReadOnly="True">  
                        <DataGridTemplateColumn.CellTemplate>  
                            <DataTemplate>  
                                <StackPanel>  
                                    <TextBlock Text="{Binding Location[0]}" Foreground="{Binding solidColorBrush[0]}"/>  
                                    <TextBlock Text="{Binding Location[1]}" Foreground="{Binding solidColorBrush[1]}"/>  
                                    <TextBlock Text="{Binding Location[2]}" Foreground="{Binding solidColorBrush[2]}"/>  
                                    <TextBlock Text="{Binding Location[3]}" Foreground="{Binding solidColorBrush[3]}"/>  
                                </StackPanel>  
                            </DataTemplate>  
                        </DataGridTemplateColumn.CellTemplate>  
                    </DataGridTemplateColumn>  
    

    C#:

    public partial class MainWindow : Window  
        {  
            ViewModel vm = new ViewModel();  
            public MainWindow()  
            {  
                InitializeComponent();  
                vm.People =  new List<MyClass>(){ new MyClass (){  
                    Location =new List<string> (){ "IP:10","S/N:6725542","Tag:602","Mod:Phaser"},  
                    solidColorBrush =new List<SolidColorBrush> (){ new SolidColorBrush (Colors.Red),new SolidColorBrush (Colors.Black),new SolidColorBrush(Colors.Yellow),new SolidColorBrush (Colors.Blue)  } },            
                };  
              
                this.DataContext = vm;  
            }  
        }  
        public class ViewModel  
        {  
            public List<MyClass> People { get; set; }  
      
            public ViewModel()  
            {  
                People = new List<MyClass>();  
                  
            }  
        }  
        public class MyClass  
        {  
            public List<string> Location { get; set; }  
            public List<SolidColorBrush> solidColorBrush { get; set; }  
        }  
    

    7579-annotation-2020-04-22-123140.png

    Thanks.