Content Only being shown in a Single element at a given time

故事扮演 提交于 2020-03-05 06:07:33

问题


I am Trying to Display a bunch of Names with an Icon in a particular Scroll viewer. There are three main types of Icons that need to be displayed , However although all 3 Types pf Icons are disgustingly being displayed only One Element gets this Icon at a time.

What I mean by this is say for example there are 6 Items being Displayed 2 From each different time. Then the Icon will only be displayed 3 times instead of 6 times . The moment I delete a particular Item of a given type then the Icon goes and attaches with the remaining item which did not have the icon before.

I cannot Understand what I am missing here

My XAML Code is as Follows,

<DataTemplate.Triggers >
            <DataTrigger Binding="{Binding Path=Model, Converter={StaticResource App_TypeOfConverter}}" Value="{x:Type models:FileSettingsModel}">
                <Setter TargetName="TypeIcon" 
                        Property="Content" 
                        Value="{StaticResource FileIcon}"/>
            </DataTrigger>

            <DataTrigger Binding="{Binding Path=Model, Converter={StaticResource App_TypeOfConverter}}" Value="{x:Type models:ServerSettingsModel}">
                <Setter TargetName="TypeIcon" 
                        Property="Content" 
                        Value="{StaticResource ServerIcon}"/>
            </DataTrigger>

            <DataTrigger Binding="{Binding Path=Model, Converter={StaticResource App_TypeOfConverter}}" Value="{x:Type models:HomeSettingsModel}">
                <Setter TargetName="TypeIcon" 
                        Property="Content" 
                        Value="{StaticResource HomeIcon}"/>
            </DataTrigger>
</DataTemplate.Triggers>

The Information is stores and Used inside this ContentControl

<ContentControl x:Name="TypeIcon" Width="16" Height="16" VerticalAlignment="Center" Margin="5,0,0,0" Focusable="False" />

I have tried this using the ContentPresenter tag and Image tag wwhich both threw exceptions

EDIT :- WPF: Can use StaticResource only once : this is Quite Similar to the Problem I have Except I cannot Use an Image Property as I am not working with Image files

Details on How the Icons are Defined The Scroll View Contains these Items which are based on 3 Different Classes namely , FileSettingsModel ,ServerSettingsModel and HomeSettingsModel

With this Current Implementation There is No problem with any relation to those classes however I believe the problem is in the content Control Not being able to Hold Multiple Childs or rather Static Resources ?

FurtherEdit

<Canvas x:Key="FileIcon" Width="12" Height="12">
 <Path Stroke="Black" Fill="White"  Data="M20,4L4,4A2,2,0,0,0,2,6L2,18A2,2,0,0,0,4,20L20,20A2,2,0,0,0,22,18L22,6A2,2,0,0,0,20,4 M20,18L4,18 4,8 12,13 20,8 20,18 M20,6L12,11 4,6 4,6 20,6 20,6z">
 </Path>
</Canvas>

Above is an Example of the icons that are being used

There are 2 more icons exactly similar for the other two types


回答1:


Here is a minimal example that verifies, that a resource with x:Shared="False" can be used to render multiple occurences of the same icon, while with a shared resource (default), only the last occurence is rendered:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        x:Class="Application1.MainWindow">
    <Window.Resources>
        <Canvas x:Key="FileIconShared" Width="20" Height="20">
            <Path Stroke="Black" Fill="White"  Data="M20,4L4,4A2,2,0,0,0,2,6L2,18A2,2,0,0,0,4,20L20,20A2,2,0,0,0,22,18L22,6A2,2,0,0,0,20,4 M20,18L4,18 4,8 12,13 20,8 20,18 M20,6L12,11 4,6 4,6 20,6 20,6z">
            </Path>
        </Canvas>
        <Canvas x:Key="FileIconNotShared" x:Shared="False" Width="20" Height="20">
            <Path Stroke="Black" Fill="White"  Data="M20,4L4,4A2,2,0,0,0,2,6L2,18A2,2,0,0,0,4,20L20,20A2,2,0,0,0,22,18L22,6A2,2,0,0,0,20,4 M20,18L4,18 4,8 12,13 20,8 20,18 M20,6L12,11 4,6 4,6 20,6 20,6z">
            </Path>
        </Canvas>
    </Window.Resources>
    <Grid>
        <StackPanel HorizontalAlignment="Left" Width="100">
            <TextBlock>Shared 1</TextBlock>
            <ContentControl Content="{StaticResource FileIconShared}"/>
            <TextBlock>Shared 2</TextBlock>
            <ContentControl Content="{StaticResource FileIconShared}"/>
            <TextBlock>Not Shared 1</TextBlock>
            <ContentControl Content="{StaticResource FileIconNotShared}"/>
            <TextBlock>Not Shared 2</TextBlock>
            <ContentControl Content="{StaticResource FileIconNotShared}"/>
        </StackPanel>
    </Grid>
</Window>

Effect: First Icon is missing, 2nd to 4th is rendered.

I can't say how you would apply this to your project, since to much information is missing on where exactly your resources are defined and how exactly your datatemplate is configured.



来源:https://stackoverflow.com/questions/57602338/content-only-being-shown-in-a-single-element-at-a-given-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!