2017-08-17 58 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,該圖像僅顯示一次。

相關問題