question

LukaCiglar-4298 avatar image
0 Votes"
LukaCiglar-4298 asked ·

Flowdocument Table - custom border style

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!

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

1 Answer

gekka avatar image
0 Votes"
gekka answered ·

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)));
     }
 }


· 1 · Share
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.

Thanks for response Gekka. Unfortunately this is not what I was hoping for. I'm looking for a solution to create border around whole Table, not just a single TableCell. And It should work for paging too.

I've tried couple of things with Adorners too, but that Is also an issue when dealing with Table that Is filled dynamically.

So I'm dropping everything, thanks for your time, cheers.

0 Votes 0 · ·