Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Label adapt FontSize to it's Width and Height
    primarykey
    data
    text
    <p>I need to develop a <code>Label</code> control in <strong>WPF</strong>, on <strong>.NET 3.5</strong> and <strong>VisualStudio 2010</strong>, in which the <code>FontSize</code> will automatically make the text fill the control area.</p> <p>I don't know if I should create a <code>CustomControl</code> inheriting from <code>Label</code> or if I should create a <code>UserControl</code> which contains a <code>Label</code> control.</p> <p>I've seen an example here using a <code>ValueConverter</code>, but I'm not understanding its behavior, here: <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/89167931-0813-4d6a-92b7-42a3323a5bd2" rel="nofollow noreferrer">change font size dynamically</a>.</p> <p>Can anyone give me a clue about that?</p> <p><strong>Update:</strong> </p> <p>I found the solutiion using the <code>DoubleConverter</code> from the example I've posted before:</p> <p>The soultion is using a <code>ValueConverter</code>, which I extracted from the example, but added the NumerFormat IFormatProvider to correctly parse "0.1" value, I found that at <a href="http://bytes.com/topic/c-sharp/answers/495594-decimal-d1-decimal-parse-0-1-1-a" rel="nofollow noreferrer">Decimal d1 = Decimal.Parse("0.1"); // = 1?!?</a>:</p> <pre><code> [ValueConversion(typeof(object), typeof(double))] public class DoubleConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { double dblValue = (double)value; double scale = Double.Parse(((string)parameter), System.Globalization.CultureInfo.InvariantCulture.NumberFormat); return dblValue * scale; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } </code></pre> <p>Then, you have to instantiate in XAML the <code>DoubleConverter</code>, and specify the binding in the <code>FonSize</code> Property:</p> <pre><code>&lt;UserControl x:Class="&lt;Namespace&gt;.LabelAutoFontSize" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:me="clr-namespace:&lt;Namespace&gt;" mc:Ignorable="d" d:DesignHeight="60" d:DesignWidth="278"&gt; &lt;UserControl.Resources&gt; &lt;me:DoubleConverter x:Key="doubleConverter" /&gt; &lt;/UserControl.Resources&gt; &lt;Grid&gt; &lt;Label x:Name="lbl" FontSize="{ Binding Path=Width, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Converter={StaticResource doubleConverter}, ConverterParameter=0.116}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Content="LabelAutoFontSize" d:LayoutOverrides="Width" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" /&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>An important point is that the value for <code>ConverterParameter</code> depends absolutely from the font assigned. Each font may need a different value and you have to "play around" to get the correct value to fit exactly.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload