Cá nhân tôi cố gắng sắp xếp bố cục của mình tốt hơn để chúng không phụ thuộc vào kích thước của nút radio. Điều đó nói rằng, nếu bạn hoàn toàn nhấn mạnh vào việc làm theo cách này thì bạn sẽ cần phải thiết lập chiều rộng của mỗi TextBlock theo kích thước của nó khi Bold, và theo cách đó sẽ làm cho nó cập nhật nếu bạn cần thay đổi văn bản và/hoặc phông chữ gia đình vv Để làm điều đó bạn sẽ cần phải ràng buộc các tài sản chiều rộng để một chuyển đổi chấp nhận tất cả những giá trị khác.
Bắt đầu với một phong cách mà bộ chiều rộng của hộp văn bản:
<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>
Bây giờ thêm mã cho bộ chuyển đổi giá trị bản thân (lưu ý rằng tôi đang sử dụng code posted by Clarke Kent in another StackOverflow answer để đo văn bản):
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);
}
}
Nhưng một lần nữa, trong một ứng dụng thực sự tôi sẽ cố gắng giải quyết điều này đúng cách bằng cách đặt chúng vào lưới hoặc thứ gì đó.
Bạn có thể hiển thị xaml cho 'RadioButtons' không? – clcto
Bạn có đang sử dụng mẫu điều khiển cho RadioButton không? –