Home > OS >  Display icon over select images in wpf listbox
Display icon over select images in wpf listbox

Time:03-18

I've looked at some related answers (enter image description here

CodePudding user response:

It looks like the problem is not related to the Viewbox but the image resource appbar_control_play it references.

There is no need to add the Viewbox via a Control. Just add it directly to the DataTemplate.

Generally prefer a ContentControl over a templated Control if you wish to display content.

The x:Shared attribute is only required on a UIElement that is not part of a template but defined in a ResourceDictionary. For example, when you define the Viewbox in as a resource, you must set the x:Shared attribute to false. Otherwise it is only allowed to appear once in the visual tree.

  1. In case the image resource is an image file, a proper DataTemplate could look as followed:
<DataTemplate>
  <StackPanel>
    <Grid>
      <Image Source="path to image" />
      <Image Source="path to overlay icon"
             Stretch="UniformToFill"
             Width="50"
             Height="50" />
    </Grid>
  </StackPanel>
</DataTemplate>
  1. In case the icon is a XAML resource like a Geometry or a Segoe MDL2 Assets font icon, the DataTemplate should look as followed:

App.xaml

<Application.Resources>
  <Viewbox x:Key="PlayIcon" x:Shared="False">
    <TextBlock FontFamily="Segoe MDL2 Assets"
               Text="&#xE768;" />
  </Viewbox>
</Application.Resources>

MyControl.xaml

<DataTemplate>
  <StackPanel>
    <Grid>
      <Image Source="path to image" />
      <ContentControl Content="{StaticResource PlayIcon}"
                      Width="50"
                      Height="50" />
    </Grid>
  </StackPanel>
</DataTemplate>
  • Related