Question

Is it possible to add null as a static resource of a markup element? I want to be able to refer to a value using {StaticResource myKey} syntax. At the moment the value I need to refer to is null, but in future it might not be. I have multiple references to the value in the rest of the markup and I would like them to refer to a resource key rather than {x:Null}.

I expected to do this:

<Window.Resources>
    <x:Null key="myKey" />
</Window.Resources>

...but that does not work. It compiles but at runtime a XamlParseException is raised saying that the resource reference cannot be resolved.

Was it helpful?

Solution

This works fine for me:

<Window x:Class="SO16456565.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <!--<SolidColorBrush x:Key="BG" Color="AntiqueWhite"/>-->
        <x:NullExtension x:Key="BG"/>
    </Window.Resources>
    <Border Background="{StaticResource BG}"/>
</Window>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top