jon-a-nygaard
jon-a-nygaard

Reputation: 63

WPF - Auto size all text

I am trying to make all elements in my application resize automatically to the parent element. And that is going well with the use of grids, margin and auto size. Problem is that the text size is not resizing and becomes looks very small inside a large element. I have tried using a viewbox with buttons which works good:

<Button x:Name="button_1">
  <Viewbox>
    <TextBlock Text="Button1"></TextBlock>
  </Viewbox>                
</Button>

But I can not get this to work with the Header in a TreeviewItem. How can i auto resize the font of all my text elements in the application? Any solutions for this are much appriciated. Does not need to involve the use of viewbox elements.

Edit: I may have formulated my question a bit unclear. My problem is to autosize the text inside the Header in a TreeViewItem.

<TreeView x:Name="tree1" Margin="0">
    <TreeViewItem x:Name="item1" Header="Item1"> //This header needs to autosize
     ...
    </TreeViewItem>
</TreeView>

Solution: Equivalent to the answer mathieu has provided.

<TreeView x:Name="tree1" Margin="0">
    <TreeViewItem x:Name="item1">
      <TreeViewItem.Header>
        <Viewbox>
          <TextBlock>Item1</TextBlock>
        </Viewbox>
      </TreeViewItem.Header>
    </TreeViewItem>
</TreeView>

Upvotes: 4

Views: 1839

Answers (2)

mathieu
mathieu

Reputation: 31202

Declare your header like this :

<TreeView x:Name="tree1" Margin="0">
    <TreeViewItem x:Name="item1">
        <TreeViewItem.Header>
            <Viewbox>
                <TextBlock TextWrapping="Wrap" Text="Header" />
            </Viewbox>   
        </TreeViewItem.Header>
    </TreeViewItem>
</TreeView>

Upvotes: 1

Freelancer
Freelancer

Reputation: 9074

Go through this code:

String text = "Your Text Goes here";
    Typeface objTypeFace = new Typeface("Helvetica");
    FormattedText objft = new FormattedText(text, CultureInfo.CurrentCulture, 
            FlowDirection.LeftToRight, objTypeFace , 20, Brushes.Red);

    Size textSize = new Size(objft.Width, objft.Height);

OR:

<Button x:Name="button_1">
  <Viewbox>
    <TextBlock TextWrapping="Wrap" Text="Button1"></TextBlock>
  </Viewbox>                
</Button>

Upvotes: 0

Related Questions