Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a simple Table in WPF?

I was wondering if there is a way (any components/controls) that allow me to draw a simple Microsoft Word style table in my application window. Something like this:

Sample Table

Any ideas?

like image 720
Mohammad Sepahvand Avatar asked Sep 12 '11 08:09

Mohammad Sepahvand


People also ask

How do I display a table in XAML?

Declaring a DataGrid in XAMLCanUserResizeColumns – allows the user to change the width of columns in the table. CanUserSortColumns – allows the user to sort the data in the table by clicking on column names. IsReadOnly – when true, prevents the user from double-clicking a cell to edit its contents.

What is a data grid in WPF?

Advertisements. A DataGrid is a control that displays data in a customizable grid. It provides a flexible way to display a collection of data in rows and columns.

How do you add rows and columns to a WPF grid programmatically?

RowDefinitions. Add(gridRow3); Once rows and columns are added to Grid, you can add any contents to Grid cells by using SetRow and SetColumn methods. SetRow and SetColumn methods take first parameter as the control name and second parameter as row number and column number respectively.

How do you make a table in UWP?

As said in the comments, you can use Telerik Grid. However, if you want to build the table yourself the Grid control will suffice. This will create, respectively, a column that sizes itself based on the amount of available space and a row that sizes itself to its content.


1 Answers

It depends on how you want to use it. Either use one of the ItemsControl (like DataGrid, ListView etc), do it directly with a Grid panel (as recommended by the other answers) or use a FlowDocument

FlowDocument allows you to specify Tables, Rows and Columns. You can also select several cells at once for Copy/Paste etc.

enter image description here

<FlowDocumentReader UseLayoutRounding="True" SnapsToDevicePixels="True">
    <FlowDocumentReader.Resources>
        <Style TargetType="TableCell">
            <Setter Property="TextAlignment" Value="Center"/>
        </Style>
    </FlowDocumentReader.Resources>
    <FlowDocument>
        <Table CellSpacing="0">
            <Table.Columns>
                <TableColumn/>
                <TableColumn/>
                <TableColumn/>
                <TableColumn/>
            </Table.Columns>
            <TableRowGroup>
                <TableRow>
                    <TableCell BorderBrush="Black" BorderThickness="1">
                        <Paragraph FontWeight="Bold">Category</Paragraph>
                    </TableCell>
                    <TableCell BorderBrush="Black" BorderThickness="0,1,1,1">
                        <Paragraph FontWeight="Bold">A</Paragraph>
                    </TableCell>
                    <TableCell BorderBrush="Black" BorderThickness="0,1,1,1">
                        <Paragraph FontWeight="Bold">B</Paragraph>
                    </TableCell>
                    <TableCell BorderBrush="Black" BorderThickness="0,1,1,1">
                        <Paragraph FontWeight="Bold">C</Paragraph>
                    </TableCell>
                </TableRow>
                <TableRow>
                    <TableCell BorderBrush="Black" BorderThickness="1,0,1,1">
                        <Paragraph FontWeight="Bold">Subscription</Paragraph>
                    </TableCell>
                    <TableCell BorderBrush="Black" BorderThickness="0,0,1,1">
                        <Paragraph>Monthly</Paragraph>
                    </TableCell>
                    <TableCell BorderBrush="Black" BorderThickness="0,0,1,1">
                        <Paragraph>Yearly</Paragraph>
                    </TableCell>
                    <TableCell BorderBrush="Black" BorderThickness="0,0,1,1">
                        <Paragraph>Monthly</Paragraph>
                    </TableCell>
                </TableRow>
                <TableRow>
                    <TableCell BorderBrush="Black" BorderThickness="1,0,1,1" TextAlignment="Center">
                        <Paragraph FontWeight="Bold">Price</Paragraph>
                    </TableCell>
                    <TableCell BorderBrush="Black" BorderThickness="0,0,1,1">
                        <Paragraph>$120.00</Paragraph>
                    </TableCell>
                    <TableCell BorderBrush="Black" BorderThickness="0,0,1,1">
                        <Paragraph>$1000.00</Paragraph>
                    </TableCell>
                    <TableCell BorderBrush="Black" BorderThickness="0,0,1,1">
                        <Paragraph>$130.00</Paragraph>
                    </TableCell>
                </TableRow>
            </TableRowGroup>
        </Table>
    </FlowDocument>
</FlowDocumentReader>

This page is full of usefull examples about this: FlowDocument with Table

like image 191
Fredrik Hedblad Avatar answered Oct 12 '22 20:10

Fredrik Hedblad