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.
<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>
using System.Windows;
using System.Data;
namespace ExamlpeForForum
{
public partial class MainWindow : Window
{
DataTable dt1 = new DataTable();
public MainWindow()
{
InitializeComponent();
dt1.Columns.Add("Column0");
dt1.Columns.Add("Column1");
dt1.Columns.Add("Column2");
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.