DataTemplate 只显示最后一条记录的 Canvas

DataTemplate only shows Canvas for the last record

我正在使用以下数据模板

<DataTemplate x:Key="platform_resources">
   <StackPanel Orientation="Horizontal">
      <Viewbox Width="30" Height="30" ToolTip="Network Domain Count" Stretch="Uniform">
           <ContentControl  DataContext="{Binding}" Focusable="False" Content="{DynamicResource appbar_server}" />
                </Viewbox>
                <TextBlock Margin="0,7,0,0" Text="{Binding Path=workload_count}"/>
                <Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
                <Viewbox Width="30" Height="30" ToolTip="Logical Network Count" Stretch="Uniform">
                    <ContentControl Focusable="False" Content="{DynamicResource appbar_network_server_connecting}" />
                </Viewbox>
                <TextBlock Margin="0,7,0,0" Text="{Binding Path=vlan_count}"/>
                <Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
                <Viewbox Width="30" Height="30" ToolTip="Network Domain Count" Stretch="Uniform">
           <ContentControl Focusable="False" Content="{DynamicResource appbar_network}" />
      </Viewbox>

     <TextBlock Margin="0,7,0,0" Text="{Binding Path=networkdomain_count}"/>

   </StackPanel>
</DataTemplate>

该模板显示所有带有分隔符的相关数据,但仅显示最后一条记录中的图像。它在应该有图像的地方留下了空间,但没有图像。

确保将 x:Shared="False" 属性 添加到您的资源中。

示例:

<Canvas x:Key="appbar_server" x:Shared="False">
   <!-- ... -->
</Canvas>

发生这种情况是因为您可能在资源中定义了一些 Image(例如 appbar_server)并试图在多个项目中显示它们。但是Image是一个Visual,在WPF中每个Visual只能有一个parent。因此,当您的项目正在生成时,每个项目都会从前一个项目中窃取 Image,直到最后一个项目最终获得它。

解法:

Image不同,BitmapImage不是Visual,因此可以多次设置为不同项目的来源。因此,不要在 Resources 中定义 Images,而是定义 BitmapImages:

<Window.Resources>
    <BitmapImage x:Key="appbar_server" UriSource="C:\...\appbar_server.png"/>
    ....

然后在 DataTemplate 中创建 Image 个实例,而不是 ContentControl 来展示它们:

            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Viewbox Width="30" Height="30" ToolTip="Network Domain Count" Stretch="Uniform">
                        <Image  Focusable="False" Source="{DynamicResource appbar_server}" />
                    </Viewbox>
                    <TextBlock Margin="0,7,0,0" Text="{Binding Path=workload_count}"/>
                    ...

*更新:

The image is captured in a canvas which seems to be needing some special wrapper to make this work.

在这种情况下,您应该为每个 Canvas 定义一个 DataTemplate,如下所示:

<Window.Resources>
    <DataTemplate x:Key="appbar_3d_3ds">
        <Canvas  Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
            <Path Width="32" Height="40" Canvas.Left="23" Canvas.Top="18" Stretch="Fill" Fill="Black" Data="F1 M 27,18L 23,26L 33,30L 24,38L 33,46L 23,50L 27,58L 45,58L 55,38L 45,18L 27,18 Z "/>
        </Canvas>
    </DataTemplate>
    ....

然后在您的 ItemTemplate 中创建 ContentPresenter 个实例,并将它们的 ContentTemplate 设置为您的 pre-defined 数据模板(例如 appbar_3d_3ds)。

            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Viewbox Width="30" Height="30" ToolTip="Network Domain Count" Stretch="Uniform">
                        <ContentPresenter ContentTemplate="{DynamicResource appbar_3d_3ds}"/>
                    </Viewbox>
                    <TextBlock Margin="0,7,0,0" Text="{Binding Path=workload_count}"/>
                    ....