Flowdocument Table - custom border style

Luka Ciglar 21 Reputation points
2020-04-10T12:23:26.977+00:00

Is there any option to create a custom border style for Wpf Table (type of Content Element for FlowDocuments), like create a rounded corners?

I'm thinking that It might be possible with drawing PathGeometry or using Adorner, but I don't know how to do that.

Any help much appreciated!

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,678 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. gekka 6,766 Reputation points MVP
    2020-04-12T03:59:19.117+00:00

    Hi LukaCiglar,

    It's not impossible, but it's difficult to do it completely ,because this cannot be handled across page boundaries.

    <Window x:Class="WpfApp1.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:WpfApp1"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
    
        <Grid >
            <FlowDocumentScrollViewer >
                <FlowDocumentScrollViewer.Document>
                    <FlowDocument >
                        <FlowDocument.Resources>
                            <Style TargetType="TableCell">
                                <Setter Property="BorderBrush" Value="Transparent"/>
                                <Setter Property="BorderThickness" Value="1,1,1,1" />
                            </Style>
                        </FlowDocument.Resources>
    
                        <Table CellSpacing="0">
                            <Table.Columns>
                                <TableColumn Width="100"/>
                                <TableColumn Width="100" />
                                <TableColumn Width="100"/>
                            </Table.Columns>
    
                            <TableRowGroup >
                                <TableRow >
                                    <TableCell RowSpan="2" BorderThickness="0" >
    
                                        <BlockUIContainer>
                                            <Canvas HorizontalAlignment="Left" VerticalAlignment="Stretch" >
                                                <Border Background="Transparent"
                                                        BorderBrush="Red"
                                                        BorderThickness="1"
                                                        CornerRadius="5,0,0,5"
                                                        Loaded="Border_Loaded"  />
                                            </Canvas>
                                        </BlockUIContainer>
                                        <Paragraph >
                                            <Run >ABCDEFGHIJKLMNOPQRSTUVWXYZ</Run>
                                        </Paragraph>
                                    </TableCell>
    
                                    <TableCell ColumnSpan="2" BorderThickness="0" >
                                        <local:BorderEx Background="LightYellow"
                                                        BorderBrush="Green"  
                                                        BorderThickness="2"
                                                        CornerRadius="10"/>
                                        <Paragraph Margin="5">
                                            <Run>ABCDEFGHIJKLMNOPQRSTUVWXYZ</Run>
                                        </Paragraph>
                                    </TableCell>
    
                                </TableRow>
    
                                <TableRow>
                                    <TableCell />
                                    <TableCell BorderBrush="Black" BorderThickness="1"/>
                                </TableRow>
                            </TableRowGroup>
                        </Table>
    
                    </FlowDocument>
                </FlowDocumentScrollViewer.Document>
            </FlowDocumentScrollViewer>
        </Grid>
    </Window>
    

    namespace WpfApp1
    {
        using System;
        using System.Collections;
        using System.Windows;
        using System.Windows.Controls;
        using System.Windows.Data;
        using System.Windows.Documents;
        using System.Windows.Media;
    
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Border_Loaded(object sender, RoutedEventArgs e)
            {
                BorderEx.Border_Loaded(sender, e);
            }
        }
    
        class BorderEx : BlockUIContainer
        {
            public static void Border_Loaded(object sender, RoutedEventArgs e)
            {
                Border border = (Border)sender;
                DependencyObject da = border;
                DependencyObject db = null;
                while (da != null)
                {
                    if (da.GetType().Name == "RowVisual")
                    {
                        var cv = db as System.Windows.Media.ContainerVisual;
                        if (cv != null)
                        {
                            border.Width = cv.DescendantBounds.Width;
                            border.Height = cv.DescendantBounds.Height;
                        }
                        break;
                    }
                    db = da;
                    da = VisualTreeHelper.GetParent(da);
                }
            }
    
            public BorderEx()
            {
                this.Loaded += BorderEx_Loaded;
            }
    
            private void BorderEx_Loaded(object sender, RoutedEventArgs e)
            {
                Border border = new Border();
                border.SetBinding(Border.BorderBrushProperty, new Binding() { Path = new PropertyPath(BorderEx.BorderBrushProperty), Source = this });
                border.SetBinding(Border.BorderThicknessProperty, new Binding() { Path = new PropertyPath(BorderEx.BorderThicknessProperty), Source = this });
                border.SetBinding(Border.CornerRadiusProperty, new Binding() { Path = new PropertyPath(BorderEx.CornerRadiusProperty), Source = this });
                border.SetBinding(Border.BackgroundProperty, new Binding() { Path = new PropertyPath(BorderEx.BackgroundProperty), Source = this });
                border.Loaded += Border_Loaded;
    
                Canvas cv = new Canvas();
                cv.Width = 0;
                cv.Height = 0;
                cv.HorizontalAlignment = HorizontalAlignment.Left;
                cv.VerticalAlignment = VerticalAlignment.Top;
                cv.Children.Add(border);
                this.Child = cv;
            }
    
            public CornerRadius CornerRadius
            {
                get { return (CornerRadius)GetValue(CornerRadiusProperty); }
                set { SetValue(CornerRadiusProperty, value); }
            }
            public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(BorderEx), new PropertyMetadata(default(CornerRadius)));
    
            public new Thickness BorderThickness
            {
                get { return (Thickness)GetValue(BorderThicknessProperty); }
                set { SetValue(BorderThicknessProperty, value); }
            }
            public new static readonly DependencyProperty BorderThicknessProperty = DependencyProperty.Register("BorderThickness", typeof(Thickness), typeof(BorderEx), new PropertyMetadata(default(Thickness)));
        }
    }