Personalmente mi piacerebbe provare a organizzare i miei layout quindi meglio che non erano dipendenti dalla dimensione dei pulsanti di opzione. Detto questo, se insisti assolutamente a farlo in questo modo, dovrai impostare la larghezza di ciascun TextBlock in base alle sue dimensioni in grassetto e in un modo che lo farà aggiornare se hai bisogno di cambiare il testo e/o famiglia di caratteri ecc. Per fare ciò è necessario associare la proprietà Width a un convertitore che accetta tutti gli altri valori.
Inizia con uno stile che imposta la larghezza della casella di testo:
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Width">
<Setter.Value>
<MultiBinding Converter="{StaticResource TextToWidthConverter}">
<Binding Path="Text" RelativeSource="{RelativeSource Self}" UpdateSourceTrigger="PropertyChanged"/>
<Binding Path="FontFamily" RelativeSource="{RelativeSource Self}" UpdateSourceTrigger="PropertyChanged"/>
<Binding Path="FontStyle" RelativeSource="{RelativeSource Self}" UpdateSourceTrigger="PropertyChanged"/>
<Binding Path="FontStretch" RelativeSource="{RelativeSource Self}" UpdateSourceTrigger="PropertyChanged"/>
<Binding Path="FontSize" RelativeSource="{RelativeSource Self}" UpdateSourceTrigger="PropertyChanged"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
Ora aggiungere il codice per il convertitore valore stesso (notare che io sto usando code posted by Clarke Kent in another StackOverflow answer per misurare il testo):
public class TextToWidthConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var text = values[0] as String;
var fontFamily = values[1] as FontFamily;
var fontStyle = (FontStyle)values[2];
var fontStretch = (FontStretch)values[3];
var fontSize = (Double)values[4];
var size = MeasureText(text, fontFamily, fontStyle, FontWeights.Bold, fontStretch, fontSize);
return size.Width;
}
public object[] ConvertBack(object values, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
/// <summary>
/// Get the required height and width of the specified text. Uses Glyph's
/// </summary>
public static Size MeasureText(string text, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, double fontSize)
{
Typeface typeface = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch);
GlyphTypeface glyphTypeface;
if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
{
return MeasureTextSize(text, fontFamily, fontStyle, fontWeight, fontStretch, fontSize);
}
double totalWidth = 0;
double height = 0;
for (int n = 0; n < text.Length; n++)
{
ushort glyphIndex = glyphTypeface.CharacterToGlyphMap[text[n]];
double width = glyphTypeface.AdvanceWidths[glyphIndex] * fontSize;
double glyphHeight = glyphTypeface.AdvanceHeights[glyphIndex] * fontSize;
if (glyphHeight > height)
{
height = glyphHeight;
}
totalWidth += width;
}
return new Size(totalWidth, height);
}
/// <summary>
/// Get the required height and width of the specified text. Uses FortammedText
/// </summary>
public static Size MeasureTextSize(string text, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, double fontSize)
{
FormattedText ft = new FormattedText(text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(fontFamily, fontStyle, fontWeight, fontStretch),
fontSize,
Brushes.Black);
return new Size(ft.Width, ft.Height);
}
}
Ma di nuovo, in una vera app proverei a risolverlo correttamente inserendoli in una griglia o qualcosa del genere.
Potete mostrare l'xaml per i 'RadioButtons'? – clcto
Stai usando un modello di controllo per i RadioButton? –