0

I need to display an information icon for some ListBox items.

The icon is defined as a ViewBox in the application resourses and I use DataTemplate as ListBox ItemTemplate.

<Viewbox x:Key="InformationSymbol" Width="16" Height="16"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Canvas Width="16" Height="16">
        <Path Fill="#00FFFFFF" Data="F1M16,16L0,16 0,0 16,0z" />
        <Path Fill="#FFF6F6F6" Data="F1M10,14L6,14 6,2 10,2z" />
        <Path Fill="#FF424242" Data="F1M9,3L7,3 7,5 9,5z M9,13L7,13 7,6 9,6z" />
    </Canvas>
</Viewbox>

<DataTemplate x:Key="MyDataTemplate">
    <!--main data here-->
    <ContentControl Content="{DynamicResource InformationSymbol}">
        <!--visibility trigger here-->
    </ContentControl>
</DataTemplate>

<ListBox ItemsSource="{Binding MyList}"
    ItemTemplate="{StaticResource MyDataTemplate}"/>

The problem is that the icon is displayed for the last item only.

I've tried replacing {DynamicResource InformationSymbol} with some static text, e.g. help and it is displayed for every item correctly. So it's only the icon resource that won't display repeatedly.

How can I make it displayed for several items?

Update

Later on I tried to use the ContentControl by its own (out of the ListBox) several time as shown below and it resulted in only the last one desplayed.

<StackPanel>
    <ContentControl Content="{DynamicResource InformationSymbol}"/>
    <ContentControl Content="{DynamicResource InformationSymbol}"/>
</StackPanel>
yaugenka
  • 2,602
  • 2
  • 22
  • 41
  • Have you tried to use `StaticResource` instead of `DynamicResource`? It can be overwritten at runtime. You can also define `InformationSymbol` at `ListBox.Resources` level – Pavel Anikhouski Oct 26 '19 at 11:48
  • I've just tried that and it doesn't resolve the issue. – yaugenka Oct 26 '19 at 14:24
  • btw if I define the icon in the ListBox.Resources it become unavailable to the DataTemplate at all. So I tried putting it into the DataTemplate.Resources instead but it doesn't resolve the issue. – yaugenka Oct 26 '19 at 14:30
  • It's quite strange, you can also look at this [thread](https://stackoverflow.com/questions/2081900/wpf-adding-an-image-to-a-listbox-itemtemplate) as well – Pavel Anikhouski Oct 26 '19 at 14:34
  • Thanks for the link, Pavel. The thing is that my image is defined as a ViewBox which cannot be used with an Image control. I've just added this info to the question for the sake of completeness. – yaugenka Oct 26 '19 at 16:07
  • Ohh, I see. You can try to wrap your Content Control into `Viewbox` at data template level – Pavel Anikhouski Oct 26 '19 at 16:12
  • Pavel, your assumption about the icon being overwritten is right. Please see the update. – yaugenka Oct 26 '19 at 17:44
  • 1
    Have finally found the solution in this thread https://stackoverflow.com/questions/42924151/content-of-a-button-style-appears-only-in-one-button-instance – yaugenka Oct 26 '19 at 18:17
  • Possible duplicate of [Content of a Button Style appears only in one Button instance](https://stackoverflow.com/questions/42924151/content-of-a-button-style-appears-only-in-one-button-instance) – yaugenka Oct 26 '19 at 18:17

0 Answers0