[C#7.0 - WPF] - 具有绑定的 DataGridTemplateColumn 中的 RichTextBox

Hui Liu-MSFT 41,146 信誉分 Microsoft 供应商
2024-04-11T06:50:57.8+00:00

我对插入并绑定到 DataGridTemplateColumn 的 RichTextBox 感到疯狂。

我有7478-immagine-2.jpg

如您所见,在 DataGridTemplateColumn 的单元格中,我有四行,我希望这些行以不同的方式着色(esch 行为一种颜色)。

这是我生成列的方式:

      <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>  

我该怎么办??

Note:此问题总结整理于:[C#7.0 - WPF] - RichTextBox in a DataGridTemplateColumn with binding

Windows Presentation Foundation
Windows Presentation Foundation
.NET Framework 的一部分,它提供统一的编程模型,用于在 Windows 上构建业务线桌面应用程序。
65 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Jiale Xue - MSFT 35,556 信誉分 Microsoft 供应商
    2024-04-11T09:17:54.6+00:00

    修复我的代码:

     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


    如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。

    注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。

    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助