具有DynamicResource的Datatrigger仅可用于ItemsControl中的最后一项
问题描述:
我的DashBoardSimpleCountObject
有2个值:MyName
和MyValue
。 我使用ObservableCollection<DashboardSimpleCountObject>
,名为MyData
。具有DynamicResource的Datatrigger仅可用于ItemsControl中的最后一项
我想展示一张图片,只要MyValue
是null
。 但是,图片(“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>
我在做什么错? 非常感谢您提前! :)
答
看来,DynamicResource
的性质导致此问题。 只需将DynamicResource
更改为StaticResource
就可以了。
因此,DataTrigger
从一开始就工作得很好。由于被加载为DynamicResource
,该图像仅显示一次。
刚刚发现,它与'DynamicResource'有关。如果我没有将'Content'设置为'DataTrigger'中的图像,但是对于某些默认文本,它应该像它应该那样工作。看来,图片只会载入一个单一的项目。 – Grent
我花了相当长的一段时间,谷歌和找到一个解决方案,但显然不知道,要寻找什么 - 因为我没有怀疑问题在于'DynamicResource'。所以,对于这个“不好”的问题抱歉 - 我下次试着做得更好。 (尽管如此,在这里发布的问题总是我最后的**吸管。) – Grent