SystemFontFamilies error when binding to combobox

前端 未结 2 1909
春和景丽
春和景丽 2020-12-11 07:02

I enumerate the list of fontfamilies and bind to combobox, the problem is when there is a font in the system that is corrupted. The whole application will crashes. Any way i

2条回答
  •  盖世英雄少女心
    2020-12-11 07:29

    Okay, 5 years later, here's another solution:

    Declare this code at the Window.Resource:

    
        
            
        
    
    
    
    
    
        
            
        
    
    

    And use this Converter:

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var list = value as IReadOnlyCollection;
    
        if (list == null)
            return DependencyProperty.UnsetValue;
    
        var returnList = new List();
        foreach (FontFamily font in list)
        {
            //Instantiate a TypeFace object with the font settings you want to use
            Typeface ltypFace = new Typeface(font, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
    
            //Try to create a GlyphTypeface object from the TypeFace object
            GlyphTypeface lglyphTypeFace;
            if (ltypFace.TryGetGlyphTypeface(out lglyphTypeFace))
            {
                returnList.Add(font);
            }
        }
    
        return returnList;
    }
    

    And apply the resources to the ComboBox:

    
        
            
        
    
    

提交回复
热议问题