1

I have a simple Grid object, that has one Child, which is a TextBlock object with some text in it. This Grid is inserted into a Canvas. At the beginning WPF calculates the width and height of the grid so that the TextBlock fits into it properly. The problem is that the user is and should be allowed to resize the Grid, which works very fine. But decreasing the Grid leads to a larger TextBlock than the Grid, so that the text is not completely visible any more. I wanted to ask if C# provides me with some function like Grid.hasOverlow() where i can check if the content is larger than the parent.

This is how i am basically adding the Textblock into the grid.

Grid grid = new Grid();
TextBlock tb = new TextBlock();
tb.Text = "Some Text";
grid.Children.Add(tb);
MyCanvas.Children.Add(grid);
Kerem
  • 489
  • 5
  • 21
  • WPF works with XAML. There's no reason to use code to create the grid, its contents or layout. The code you posted is equivalent to an empty `` tag without columns or rows. You can define columns and rows with fixed or proportional sizes, include splitters etc – Panagiotis Kanavos May 09 '19 at 16:16

2 Answers2

0

I don't know about any Grid.hasOverflow type of function, but when you resize you could probably just create a delegate callback that your text object refers to. EX.

private delegate void OnGridResized(float newsizeX, float newsizeY);

private OnGridResized Callback;

//Your Function call
if (Callback != null) Callback.Invoke();

Then just subscribe your text resize event to that on awake

Main()
{
Callback += Text.Resize;
}

Don't know if this helps but I hope it works for you!

  • 1
    Thank you for your fast answer. I am quite new to C# and did't understand your code completely. Sorry for that. – Kerem May 09 '19 at 16:13
0

While asking the question i wondered if checking the height and width of both parent and child would work out and decrease the fontsize until the height and width of the child is not bigger than the height and width of the parent and it seems like it actually did work out.

Thats what how i have solved it.

if(grid.Children.Count > 0)
            {
                if (grid.Children[0].GetType().Equals(typeof(TextBlock)))
                {
                    TextBlock tb = (TextBlock)grid.Children[0];
                    if(grid.ActualHeight < tb.ActualHeight || grid.ActualWidth < tb.ActualWidth)
                    {
                        do
                        {
                            tb.FontSize -= 0.1;
                            grid.UpdateLayout();
                        } while (grid.ActualHeight < tb.ActualHeight || grid.ActualWidth < tb.ActualWidth);
                    }
                }
            }

Kerem
  • 489
  • 5
  • 21
  • Use XAML instead of writing such code. What you try to do is already available using grid row and column definitions, splitters etc – Panagiotis Kanavos May 09 '19 at 16:17
  • For example, if you specified rows and columns in XAML, you can specify the row/column on which the Textbox appears. Resizing the grid will resize the textbox automatically – Panagiotis Kanavos May 09 '19 at 16:19