How to automatically scale font size for a group of controls?

后端 未结 6 2061
予麋鹿
予麋鹿 2020-12-04 06:55

I have a few TextBlocks in WPF in a Grid that I would like to scale depending on their available width / height. When I searched for automatically scaling Font size the typi

6条回答
  •  臣服心动
    2020-12-04 07:54

    I think I know the way to go and will leave the rest to you. In this example, I bound the FontSize to the ActualHeight of the TextBlock, using a converter (the converter is below):

    
    
        
            
        
        
            
            
        
    
        
            
            
                
                
                
            
            
            
            
            
        
    
    
    
    [ValueConversion(typeof(double), typeof(double))]
    class HeightToFontSizeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // here you can use the parameter that you can give in here via setting , ConverterParameter='something'} or use any nice login with the VisualTreeHelper to make a better return value, or maybe even just hardcode some max values if you like
            var height = (double)value;
            return .65 * height;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

提交回复
热议问题