【问题标题】:WPF: Add null value as a static resourceWPF:将空值添加为静态资源
【发布时间】:2013-05-09 07:24:17
【问题描述】:
是否可以将 null 添加为标记元素的静态资源?我希望能够使用 {StaticResource myKey} 语法来引用一个值。目前我需要引用的值为 null,但将来可能不会。我在标记的其余部分中多次引用了该值,我希望它们引用资源键而不是 {x:Null}。
我希望这样做:
<Window.Resources>
<x:Null key="myKey" />
</Window.Resources>
...但这不起作用。它可以编译,但在运行时会引发 XamlParseException,表示无法解析资源引用。
【问题讨论】:
标签:
wpf
xaml
null
staticresource
【解决方案1】:
这对我来说很好用:
<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>