2017-08-17 85 views
-1

我的DashBoardSimpleCountObject有2个值:MyNameMyValue。 我使用ObservableCollection<DashboardSimpleCountObject>,名为MyData具有DynamicResource的Datatrigger仅可用于ItemsControl中的最后一项

我想展示一张图片,只要MyValuenull。 但是,图片(“loading”)仅显示在我的ObservableCollection的最后一项(无论其中有多少项)。只要设置了MyValue(与null以外的任何其他设置),它就会自动更新并正确显示 - 对所有项目都能正常工作。

<ItemsControl x:Name="_control" ItemsSource="{Binding MyData}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <WrapPanel /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Grid Margin="25"> 
       <Label FontSize="14" Content="{Binding MyName}" /> 
       <Label Margin="0,25" HorizontalContentAlignment="Right" FontSize="29" FontWeight="ExtraBold" Foreground="{Binding MyColor}"> 
        <Label.Style> 
         <Style TargetType="{x:Type Label}"> 
          <Setter Property="Content" Value="{Binding MyValue}" /> 
           <Style.Triggers> 
           <DataTrigger Binding="{Binding MyValue}" Value="{x:Null}"> 
            <Setter Property="Content"> 
             <Setter.Value> 
              <Image Width="32" Height="32" Source="{DynamicResource loading}" /> 
             </Setter.Value> 
            </Setter> 
           </DataTrigger> 
          </Style.Triggers> 
         </Style> 
        </Label.Style> 
       </Label> 
      </Grid> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

我在做什么错? 非常感谢您提前! :)

+0

刚刚发现,它与'DynamicResource'有关。如果我没有将'Content'设置为'DataTrigger'中的图像,但是对于某些默认文本,它应该像它应该那样工作。看来,图片只会载入一个单一的项目。 – Grent

+0

我花了相当长的一段时间,谷歌和找到一个解决方案,但显然不知道,要寻找什么 - 因为我没有怀疑问题在于'DynamicResource'。所以,对于这个“不好”的问题抱歉 - 我下次试着做得更好。 (尽管如此,在这里发布的问题总是我最后的**吸管。) – Grent

回答

0

看来,DynamicResource的性质导致此问题。 只需将DynamicResource更改为StaticResource就可以了。

因此,DataTrigger从一开始就工作得很好。由于被加载为DynamicResource,该图像仅显示一次。

相关问题