wpf - 有时 ListView 中的图像会消失
问题描述
带有国旗的国家列表显示在 ListView 中。标志显示为带有填充 ImageBrush 的椭圆。
<ControlTemplate TargetType="ContentControl">
<StackPanel Orientation="Horizontal" Background="Transparent">
<Ellipse Width="23" Height="23" StrokeThickness="1" Stroke="#2F7AE5" Fill="{Binding FlagBrush}"/>
<TextBlock Text="{Binding Path=Country}"
Foreground="{StaticResource HBBtnTextBrush}" FontSize="16" FontWeight="Bold"
VerticalAlignment="Center" Margin="14,0,0,0"/>
<ContentPresenter/>
</StackPanel>
</ControlTemplate>
public ImageSource FlagImage { get => Flag.GetImage(country_code); }
ImageBrush flagBrush;
public ImageBrush FlagBrush {
get
{
if (flagBrush == null)
{
flagBrush = new ImageBrush
{
ImageSource = FlagImage,
Stretch = Stretch.UniformToFill
};
flagBrush.Freeze();
}
return flagBrush;
}
}
列表隐藏部分中的一两个图像消失。图像作为带有 GeometryDrawing 的 DrawingImage 存储在资源中。如果你转到另一个页面然后返回这些图像会出现,但其他的可能会消失。在调试器中,这些图像是 ImageSource 中固有的,如果您更改任何图像参数,例如位置或调整大小,图像会立即出现。
解决方案
用肮脏的黑客解决了这个问题。我将 WrapPanel 添加到屏幕的空白区域,并在那里显示所有可能的 1x1 像素和 Opacity=0.01 的标志。在那之后,消失的标志就没有问题了。
推荐阅读
- python - Ttk 按钮按下时进度条不确定
- java - 我正在设计自定义标题栏。但标题栏不能覆盖所有屏幕。看看
- coldfusion - cfinput 不接受 .education 域名
- javascript - 使用 Ajax 提交表单两次
- apache-spark - Spark结构化流如何以Protobuf格式写入Kafka
- java - 如何在列表中获取数组值
- node.js - pdf-merger-js 抛出 TypeError 并失败
- r - 用因子水平替换一系列变量中的 NA
- css - 如何在 NextJS 中正确地进行生产构建?
- jquery - Jquery 滚动到 ID 偏移量:桌面和移动设备之间的不同偏移量