在我的应用程序中,我通过将图像控件的源代码绑定到ImageSource来显示从外部DLL获得的图像。
这工作得很好,但有时我无法从我的DLL中获得任何数据,我只想显示一张黑色图像。在这种情况下,如何创建只包含纯色的ImageSource?
发布于 2010-01-28 01:50:11
另一种方法是给出背景颜色,而不显示图像。
// XAML
<Grid Background="Black">
<Image x:Name="imgDisplay"/>
</Grid>
// C#
imgDisplay.Source = null;
// -- OR --
imgDisplay.Visibility = Visibility.Collapsed;发布于 2010-01-28 01:49:33
例如,在模板中的某处有图像,它绑定到一些属性Photo。在失败的情况下,您可以返回空值。
<Image Source="{Binding Path=Photo, IsAsync=True, TargetNullValue={StaticResource EmptyImageDrawing}}"/>在你需要的资源中的某个地方
<DrawingImage
x:Key="EmptyImageDrawing">
<DrawingImage.Drawing>
<DrawingGroup>
<GeometryDrawing>
<GeometryDrawing.Brush>
<VisualBrush
AlignmentX="Center"
AlignmentY="Center"
Stretch="None">
<VisualBrush.Visual>
<TextBlock
Text="Failed to load photo"
FontFamily="Calibri"
FontSize="70"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
TextAlignment="Center"
TextWrapping="Wrap"/>
</VisualBrush.Visual>
</VisualBrush>
</GeometryDrawing.Brush>
<GeometryDrawing.Geometry>
<RectangleGeometry
Rect="0,0,1920,1080" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>https://stackoverflow.com/questions/2149034
复制相似问题