Xaml 带有DynamicSource的Datatrigger仅适用于ItemsControl中的最后一项
MyXaml 带有DynamicSource的Datatrigger仅适用于ItemsControl中的最后一项,xaml,datatemplate,itemscontrol,datatrigger,dynamicresource,Xaml,Datatemplate,Itemscontrol,Datatrigger,Dynamicresource,My仪表板SimpleCountObject有两个值:MyName和MyValue。 我使用一个名为MyData的ObservableCollection 我想显示一张图片,只要MyValue为null。 但是,图片(“加载”)仅显示在我的可观察集合的最后一项中(无论其中有多少项)。一旦设置了MyValue(除null之外的任何内容),它就会自动更新并正确显示-这在所有项目上都可以正常工作 <ItemsControl x:Name="_control" ItemsSource="{Bind
仪表板SimpleCountObject
有两个值:MyName
和MyValue
。
我使用一个名为MyData
的ObservableCollection
我想显示一张图片,只要MyValue
为null
。
但是,图片(“加载
”)仅显示在我的可观察集合
的最后一项中(无论其中有多少项)。一旦设置了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
有关。如果我没有将内容
设置为数据触发器
中的图像,而是设置为某个默认文本,它的工作原理与它应该的一样。看起来,图片只能加载到一个项目中。我花了相当长的时间在谷歌上找到了一个解决方案,但显然不知道该寻找什么-因为我不怀疑DynamicResource
存在问题。所以,对于这个“糟糕”的问题,我很抱歉——我想下次做得更好。(尽管如此,在这里发布问题始终是我的最后一根稻草。)