WPF: adding a null value as a static resource

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 the {StaticResource myKey} syntax. At the moment, the value I need to specify is null, but in the future it may not be. I have a few references to the value in the rest of the markup, and I would like them to refer to the resource key, and not to {x:Null} .

I expected to do this:

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

... but it does not work. It compiles, but at runtime, an XamlParseException is thrown saying that the resource reference cannot be resolved.

+4
source share
1 answer

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> 
+6
source

Source: https://habr.com/ru/post/1479872/


All Articles