-2

A list of countries with flags is displayed in ListView. Flags are displayed as an ellipse with Fill 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;
    }            
}

One or two images in the hidden part of the list disappear. Images are stored in resources as DrawingImage with GeometryDrawing. If you go to another page and then return these images will appear, but others may disappear. In the debugger, these images are inherent in ImageSource, and if you change any image parameter, for example, position or resize, the image immediately appears.

Example

In Live Property Explorer

  • Is there a specific reason why you have that FlagBrush property? You could as well declare the ImageBrush in XAML and bind its ImageSource property to FlagImage. – Clemens Nov 10 '20 at 16:11
  • The list is dynamic. It is generated and can change while the application is running. In addition, the list may contain several identical countries. Flag images are also used elsewhere in the application. All countries are stored in one place in a single copy. The list contains objects-country where the brush is a property. – Vladimir Yudin Nov 10 '20 at 16:15
  • What you are showing here looks ok. There is nothing that would explain the observed behaviour. – Clemens Nov 10 '20 at 16:19
  • The global list of countries is also not permanent. – Vladimir Yudin Nov 10 '20 at 16:19
  • All that does not sound like a reason to have that FlagBrush property. However, it should work anyway. – Clemens Nov 10 '20 at 16:20
  • I will try to declare ImageBrush in XAML. I'll let you know what happens. – Vladimir Yudin Nov 10 '20 at 16:23
  • As expected, the declaration of ImageBrush in XAML did not change anything. – Vladimir Yudin Nov 11 '20 at 07:02
  • As said, there is nothing in your question that would indicate an error. We can't help you with this unless you provide more detail. – Clemens Nov 11 '20 at 08:29
  • I do not know what other details can be provided on this problem. It is not constant, some testers do not have it at all. – Vladimir Yudin Nov 11 '20 at 10:22

1 Answers1

0

Solved the problem with a dirty hack. I added WrapPanel to the screen in an empty area and displayed there all possible flags of 1x1 pixel and Opacity=0.01. After that, there was no problem with disappearing flags.