WPF converter resource makes unrelated UI content disappear

I’ve been a WPF developer for about 10 years now, and I’ve seen a lot of idiocy from it in all shapes and sizes, but today I just stumbled upon a fresh batch of male cow manure that just absolutely takes the cake. Every cake in existence, actually.

Putting a converter at the top of a Window’s resources list somehow breaks resources of UI objects that come after it.

Here is the simplest example of an application that reproduces this issue, with the least amount of code I can muster:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:WpfApp1.Converters"
xmlns:system="clr-namespace:System;assembly=mscorlib"
SizeToContent="WidthAndHeight"
WindowStartupLocation="CenterScreen">
<Window.Resources>
<converters:BaseConverter x:Key="test"/>
<Grid x:Key="TestItemTemplateGrid" x:Shared="false">
<Button Content="{Binding StringFormat='Button {0}'}" Width="300"/>
</Grid>
</Window.Resources>
<ItemsControl>
<system:Int32>0</system:Int32>
<system:Int32>1</system:Int32>
<system:Int32>2</system:Int32>
<system:Int32>3</system:Int32>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl Content="{StaticResource TestItemTemplateGrid}" Margin="0,5,0,0"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Window>
</code>
<code><Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:converters="clr-namespace:WpfApp1.Converters" xmlns:system="clr-namespace:System;assembly=mscorlib" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen"> <Window.Resources> <converters:BaseConverter x:Key="test"/> <Grid x:Key="TestItemTemplateGrid" x:Shared="false"> <Button Content="{Binding StringFormat='Button {0}'}" Width="300"/> </Grid> </Window.Resources> <ItemsControl> <system:Int32>0</system:Int32> <system:Int32>1</system:Int32> <system:Int32>2</system:Int32> <system:Int32>3</system:Int32> <ItemsControl.ItemTemplate> <DataTemplate> <ContentControl Content="{StaticResource TestItemTemplateGrid}" Margin="0,5,0,0"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </Window> </code>
<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:converters="clr-namespace:WpfApp1.Converters"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        SizeToContent="WidthAndHeight"
        WindowStartupLocation="CenterScreen">
    <Window.Resources>
        <converters:BaseConverter x:Key="test"/>

        <Grid x:Key="TestItemTemplateGrid" x:Shared="false">
            <Button Content="{Binding StringFormat='Button {0}'}" Width="300"/>
        </Grid>
    </Window.Resources>

    <ItemsControl>
        <system:Int32>0</system:Int32>
        <system:Int32>1</system:Int32>
        <system:Int32>2</system:Int32>
        <system:Int32>3</system:Int32>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ContentControl Content="{StaticResource TestItemTemplateGrid}" Margin="0,5,0,0"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

The converter:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;
namespace WpfApp1.Converters
{
public class BaseConverter : MarkupExtension, IValueConverter
{
public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Binding.DoNothing;
}
public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Binding.DoNothing;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}
</code>
<code>using System; using System.Globalization; using System.Windows.Data; using System.Windows.Markup; namespace WpfApp1.Converters { public class BaseConverter : MarkupExtension, IValueConverter { public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return Binding.DoNothing; } public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return Binding.DoNothing; } public override object ProvideValue(IServiceProvider serviceProvider) { return this; } } } </code>
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;

namespace WpfApp1.Converters
{
    public class BaseConverter : MarkupExtension, IValueConverter
    {
        public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }

        public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }
}

This is just a simple attempt to load a variable amount of items inside an ItemsControl, where each item is a ContentControl that populates itself to be a Grid defined in a resource.

This is what the app is supposed to look like based on this xaml:

wpf app 1

But here is what it actually looks like:

wpf app 2

It’s like the x:Shared is breaking and becoming true or something, just because a converter resource came before it.

Things I’ve tested:

  • The converter’s key name doesn’t matter
  • The converter’s type and/or method implementations don’t matter
  • The ItemsControl having a bound ItemsSource vs hard-coded entries doesn’t matter
  • The actual content inside the Grid doesn’t matter

Of course, removing the <converters:BaseConverter x:Key="test"/> fixes it, but here’s the real kicker: Putting it last in the resources list instead ALSO fixes it.

…Wow. Just, wow. What is this? Am I dead? Am I dreaming? Is April fools day programmed into WPF, but it’s bugged (like everything else) and came a month late? Can someone explain this? I’m losing my mind here.

The project file, in case it matters:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>
</code>
<code><Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>net5.0-windows</TargetFramework> <UseWPF>true</UseWPF> </PropertyGroup> </Project> </code>
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

</Project>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật