看来这是无法做到的。我已经尝试将 DrawingBrush 移动到 ResourceDictionary,一旦完成,数据绑定就会引发错误。如果我采用以下 XAML
<Rectangle x:Name="IconBrush" local:Helper.Colour="Black">
<Rectangle.Fill>
<DrawingBrush Stretch="Uniform">
<DrawingBrush.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Geometry="M1,1 L9.5,1 L9.5,13 L1,13 z M10.5,10.4 L13.15,13 M11.6,7.675 C11.6,9.5113467 10.066575,11 8.175,11 C6.2834248,11 4.75,9.5113467 4.75,7.675 C4.75,5.8386532 6.2834248,4.35 8.175,4.35 C10.066575,4.35 11.6,5.8386532 11.6,7.675 z">
<GeometryDrawing.Pen>
<Pen Thickness="1"
Brush="{Binding Path=(local:Helper.Colour), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Rectangle}}}" />
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Rectangle.Fill>
</Rectangle>
然后改为这个,然后它停止工作。不知道为什么不能这样来利用可重用资源。
<Rectangle x:Name="IconBrush" pm4uc:Helper.Colour="Black"
Fill="{DynamicResource NewIconBrush}" />
我猜 WPF 无法解决在资源字典中查找祖先或数据绑定的问题。