Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this example I have a simple DataTable with headings, 3 column and 2 Rows of data. Bound to datatable 1 (dt1).
What I wish to do is have the font size of each cell to adjust to fill the cell width.
No word wrapping or use of viewbox as they do not suit the overall application.

I have used viewbox on the last column to demonstrate the viewbox behaviour I wish to escape. It reduces the area of the textbox in the cell meaning that you have to click exactly on the text in order to edit the cell.

The behaviour I am looking for is to adjust the font size (doesn't matter how small it gets as in the final implementation the user can adjust the width of the column and so increase the font to a readable size) to fit the cell width on one line.

The font size just needs to adjust to the width of the cell so as to fit in the cell no matter how small the font may become.

Thank you Tony.

HTML
<Window x:Class="ExamlpeForForum.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:ExamlpeForForum"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>


        

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
        
        <DataGrid x:Name="dataGrid1" Grid.Column="2" Grid.ColumnSpan="6" Grid.Row="2" Grid.RowSpan="6" AutoGenerateColumns="False" CanUserAddRows="False" CanUserResizeColumns="True" CanUserDeleteRows="False" ScrollViewer.CanContentScroll="False" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Hidden" FontSize="12" RowHeaderWidth="0" >
        <DataGrid.Columns >
            <DataGridTemplateColumn Header="Column0" Width="50*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Column0}" TextWrapping="Wrap" FontSize="12"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Column1" Width="50*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Column1}" TextWrapping="Wrap" FontSize="12"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Column2" Width="50*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Viewbox Stretch="Fill" StretchDirection="DownOnly">
                            <TextBox Text="{Binding Column2}" TextWrapping="Wrap" FontSize="12" />
                            </Viewbox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            
        </DataGrid.Columns>
            
                
        </DataGrid>

    </Grid>
</Window>

C#
using System.Windows;
using System.Data;

namespace ExamlpeForForum
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DataTable dt1 = new DataTable();

public MainWindow()
{
InitializeComponent();




//Build Data table1 Columns
dt1.Columns.Add("Column0");
dt1.Columns.Add("Column1");
dt1.Columns.Add("Column2");

//Populate Data Table1
dt1.Rows.Add();
dt1.Rows[0][0] = "1";
dt1.Rows[0][1] = "2";
dt1.Rows[0][2] = "3";
dt1.Rows.Add();
dt1.Rows[1][0] = "4";
dt1.Rows[1][1] = "5";
dt1.Rows[1][2] = "6";

dataGrid1.ItemsSource = dt1.DefaultView;

}
}
}


What I have tried:

I have tried measuring the cell and adjusting the font , but I have had no success.
Posted
Updated 18-Nov-19 11:18am
v3
Comments
[no name] 16-Nov-19 8:32am    
There is no automatic "font size changing feature" ... the "view box" (per cell) is exactly what you need the way you describe it. And all the "10*" are redundant; using just "*" in this case accomplishes the same thing.
Member 12540636 17-Nov-19 5:20am    
Thank you for your response Gerry. And yes I have replaced the 10* with just *.
I have also updated the question to more fully explain what I am trying to achieve, the view box comes very close to what I am after but reduces the size of the text box and in doing so reduces the area to click on. I have given column3 a view box to demonstrate what I am trying to avoid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900