question

BlackSun-5322 avatar image
0 Votes"
BlackSun-5322 asked ·

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

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-wpf
immagine-2.jpg (15.3 KiB)
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AlexLi-MSFT avatar image
0 Votes"
AlexLi-MSFT answered ·

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




· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
it works :-)

thanks you

0 Votes 0 ·
LloydSheen-3317 avatar image
0 Votes"
LloydSheen-3317 answered ·

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

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
I will try this solution.

Thank you
BlackSun

0 Votes 0 ·
AlexLi-MSFT avatar image
0 Votes"
AlexLi-MSFT answered ·

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.





· 4 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
It's a good workaround for my need.

Thank you :-)

Cheers
Blacksun

0 Votes 0 ·

Hi,
your solution is good and working but... they changed their minds about tyhe look! grrrr....
Now I must do "IP, S/N, Tag, Model" in bold and with different color from their values (no comment about that decision!) and if there's no value, no line.

Is there a way to get better your solution?
Maybe really a RichTextBox?


Thank you for your help.
BlackSun

0 Votes 0 ·

The value what you said is like this? solidColorBrush =new List<SolidColorBrush> (){ new SolidColorBrush (Color.FromRgb(0,0,0)),new SolidColorBrush (Color.FromRgb(100,100,100)),new SolidColorBrush(Color.FromRgb(200, 200, 200)),new SolidColorBrush (Color.FromRgb(250, 250, 250)) }

0 Votes 0 ·

Hi,
I would like something like that:
7605-ip.jpg



Thank you for your help

0 Votes 0 ·
ip.jpg (10.3 KiB)