2009-06-04 52 views

回答

1

而不是在DataTemplate中爲您的項目設置標籤,您應該查看通過樣式設置標籤。我們可以將樣式和模板應用於我們的項目,並且它們不會互相干擾。因爲我們的項目將在ListBox內部,所以它們將自動包裝在ListBoxItem中,並且我們可以使用我們的樣式來定位該類型。

這裏我們將一些DataTemplate應用於項目(在某處定義爲資源)並使用樣式爲此ListBox中的每個項目設置標記值。

 <ListBox ItemsSource="{Binding MyItems}" ItemTemplate="{StaticResource MyDataTemplate}"> 
      <ListBox.ItemContainerStyle> 
       <Style TargetType="{x:Type ListBoxItem}"> 
        <Setter Property="Tag" 
          Value="It has a Tag" /> 
       </Style> 
      </ListBox.ItemContainerStyle> 
     </ListBox> 
相關問題