dotnet/wpf · error · ArgumentException

SR.CompositeFont_DuplicateTypeface

Error message

SR.CompositeFont_DuplicateTypeface

What it means

FamilyTypefaceCollection forbids duplicate typefaces: two entries with the same Style+Weight+Stretch combination. InsertItem runs FindItem(item) before inserting and throws ArgumentException with CompositeFont_DuplicateTypeface if an equivalent entry already exists.

Solutions

  1. Check the collection first with FindName-equivalent search (or iterate) and only add when no matching Style/Weight/Stretch exists.
  2. Replace the existing entry (Remove then Add) if you intend to override its metrics.
  3. Fix duplicate typeface declarations in the composite font definition (XAML/ConfigFile).

Example fix

// before
collection.Add(new FamilyTypeface { Style = FontStyles.Normal, Weight = FontWeights.Regular, Stretch = FontStretches.Normal });
// after
bool exists = collection.Any(t => t.Style == FontStyles.Normal && t.Weight == FontWeights.Regular && t.Stretch == FontStretches.Normal);
if (!exists) collection.Add(newFamilyTypeface);
Defensive patterns

Strategy: validation

Validate before calling

bool duplicate = collection.Any(t => t.Style == item.Style && t.Weight == item.Weight && t.Stretch == item.Stretch);
if (duplicate) return; // or replace the existing entry

Try / catch

try { collection.Add(item); }
catch (ArgumentException ex) when (ex.Message.Contains("DuplicateTypeface") || ex.Message.Contains("duplicate")) { /* same style/weight/stretch exists */ }

Prevention

When it happens

Trigger: Calling Add or Insert with a FamilyTypeface whose (Style, Weight, Stretch) tuple matches an existing entry in the collection; also occurs during composite-font parsing when the ConfigFile/FontFamily section declares the same combination twice.

Common situations: Compositely defined fonts (FontFamily.CompositeFont) in XAML where two <FontFamilyMap>-level typeface entries repeat a style/weight/stretch triple, or programmatic addition without checking for an existing match.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/fe7a8174156803f0. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyTypefaceCollection.cs:241

        }

        #endregion

        #region Internal implementation

        private int InsertItem(int index, FamilyTypeface item)
        {
            ArgumentNullException.ThrowIfNull(item);

            VerifyChangeable();

            // Validate the index.
            ArgumentOutOfRangeException.ThrowIfNegative(index);
            ArgumentOutOfRangeException.ThrowIfGreaterThan(index, Count);

            // We can't have two items with same style, weight, stretch.
            if (FindItem(item) >= 0)
                throw new ArgumentException(SR.CompositeFont_DuplicateTypeface);

            // Make room for the new item.
            if (_items == null)
            {
                _items = new FamilyTypeface[InitialCapacity];
            }
            else if (_count == _items.Length)
            {
                FamilyTypeface[] items = new FamilyTypeface[_count * 2];
                for (int i = 0; i < index; ++i)
                    items[i] = _items[i];
                for (int i = index; i < _count; ++i)
                    items[i + 1] = _items[i];
                _items = items;
            }
            else if (index < _count)
            {
                for (int i = _count - 1; i >= index; --i)

View on GitHub (pinned to 81131a70a4)