强制使用设置x:Shared =“ False”是不可能的

时间:2019-08-22 14:13:47

标签: c# wpf xaml contentpresenter

该问题可能与诸如Error when using x:Shared="False" resources in external assembly in WPF之类的问题非常相似,但是我一直找不到能够与之联系或者更确切地说是如何解决此问题的解决方案。

为什么我在标题中提到了“强制使用”,除了使用“将X:shared设置为false”之外,我无法找到其他方法来解决我的问题。

我的问题是,在具有图标的特定视图中,对于相似类型的元素,图标似乎正在共享,因此,即使存在两种或多种类型,该图标也只会显示一个项目

此问题在这些Stack oveerflow问题中也是合理的

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

WPF: Can use StaticResource only once

对于克服此问题的任何帮助/建议,我将非常感谢,下面列出了我的代码。

<DataTemplate x:Key="SettingsListViewModelDataTemplate" DataType="{x:Type viewModelsDialogsManageSettings:SettingsListItemViewModel}">


    <DataTemplate.Resources>
                <Canvas x:Key="FileSystemIcon" x:Shared="False"  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>

                <Canvas x:Key="ServerIcon" x:Shared="False"  Width="12" Height="12">
                    <Path Stroke="Black" Fill="White" Data="M17,18L12,15.82 7,18 7,5 17,5 M17,3L7,3A2,2,0,0,0,5,5L5,21 12,18 19,21 19,5C19,3.89,18.1,3,17,3z">

                    </Path>
                </Canvas>

                <Canvas x:Key="HomeIcon" x:Shared="False"   Width="12" Height="12">
                    <Canvas>
                        <Path Stroke="Green" Fill="Wheat" Data="M17,17A5,5,0,0,1,12,22A5,5,0,0,1,7,17C7,15.36,7.79,13.91,9,13L9,5A3,3,0,0,1,12,2A3,3,0,0,1,15,5L15,13C16.21,13.91,17,15.36,17,17 M11,8L11,14.17C9.83,14.58,9,15.69,9,17A3,3,0,0,0,12,20A3,3,0,0,0,15,17C15,15.69,14.17,14.58,13,14.17L13,8 11,8z"></Path>
                        <Path Stroke="Red" Fill="Lavender" Data="M17,18L12,15.82 7,18 7,5 17,5 M17,3L7,3A2,2,0,0,0,5,5L5,21 12,18 19,21 19,5C19,3.89,18.1,3,17,3z" />
                    </Canvas>
                </Canvas>

    </DataTemplate.Resources>

<!--This is Where the Conent that is being rendered to the UI-->

<StackPanel Grid.Column="0" Orientation="Horizontal">
    <ContentControl x:Name="SettingsListViewModelDataTemplate_TypeIcon" Width="12" Height="12" VerticalAlignment="Center" Margin="5,0,0,0" Visibility="Visible">
    <TextBlock Text="{Binding Name}"
                                TextTrimming="WordEllipsis"
                                VerticalAlignment="Center" 
                                Margin="5,0,0,0"/>
</StackPanel>

<!--Data Triggers for the three types-->

            <DataTemplate.Triggers >
                <DataTrigger Binding="{Binding Path=SettingsModel, Converter={StaticResource Application_TypeOfConverter}}" Value="{x:Type models:FileSettingsModel}">

                    <Setter TargetName="SettingsListViewModelDataTemplate_TypeIcon" 
                            Property="Content" 
                            Value="{StaticResource FileSystemIcon}"/>
                </DataTrigger>

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

                <DataTrigger Binding="{Binding Path=RepositorySettingsModel, Converter={StaticResource Application_TypeOfConverter}}" Value="{x:Type models:HomeSettingsModel}">
                    <Setter TargetName="SettingsListViewModelDataTemplate_TypeIcon" 
                            Property="Content" 
                            Value="{StaticResource HomeIcon}"/>
                </DataTrigger>


 </DataTemplate>

2 个答案:

答案 0 :(得分:1)

一个复杂的问题是您那里有多种颜色。如今的趋势是仅具有背景和前景的简单图标。您可以“仅”使用一种路径和几何形状。

看到这是几种形状和几种颜色,可以使用DrawingImage。

我在一个用户控件中完成了此示例,它就像您的图标之一。

       xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" 
        mc:Ignorable="PresentationOptions"
                 >
        <UserControl.Resources>
            <DrawingImage x:Key='icon'  PresentationOptions:Freeze="True">
                <DrawingImage.Drawing>
                    <DrawingGroup>
                        <DrawingGroup.Children>
                            <GeometryDrawing Geometry="M17,17A5,5,0,0,1,12,22A5,5,0,0,1,7,17C7,15.36,7.79,13.91,9,13L9,5A3,3,0,0,1,12,2A3,3,0,0,1,15,5L15,13C16.21,13.91,17,15.36,17,17 M11,8L11,14.17C9.83,14.58,9,15.69,9,17A3,3,0,0,0,12,20A3,3,0,0,0,15,17C15,15.69,14.17,14.58,13,14.17L13,8 11,8z"
                                             Brush="Wheat"
                                             >
                                <GeometryDrawing.Pen>
                                    <Pen Thickness="1" Brush="Green"/>
                                </GeometryDrawing.Pen>
                            </GeometryDrawing>
                            <GeometryDrawing Geometry="M17,18L12,15.82 7,18 7,5 17,5 M17,3L7,3A2,2,0,0,0,5,5L5,21 12,18 19,21 19,5C19,3.89,18.1,3,17,3z"
                                             Brush="Lavender"
                                             >
                                <GeometryDrawing.Pen>
                                    <Pen Thickness="1" Brush="Red"/>
                                </GeometryDrawing.Pen>
                            </GeometryDrawing>
                        </DrawingGroup.Children>
                    </DrawingGroup>
                </DrawingImage.Drawing>
            </DrawingImage>
        </UserControl.Resources>
        <Grid>
            <StackPanel>
                <Image  Width="12" Height="12" Source="{StaticResource icon}"/>
                <Image  Width="12" Height="12" Source="{StaticResource icon}"/>
            </StackPanel>

        </Grid>
    </UserControl>

答案 1 :(得分:1)

如果出于某种原因x:Shared是一个问题,我可能会考虑使用DataTemplates创建图标。 DataTemplate在应用内容时实例化内容的副本,因此共享不是问题。

资源:

<DataTemplate x:Key="FileSystemIcon">
    <Canvas 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" 
            />
    </Canvas>
</DataTemplate>

用法:

<UserControl ContentTemplate="{StaticResource FileSystemIcon}" />