2010-03-16 53 views
13

好吧,這是一個令人難以置信的簡單問題,但卻讓我發瘋。我正在學習DataTemplating,並試圖將非常簡單的ItemTemplate應用於ListBox。WPF - 非常基本的ListBox.ItemTemplate問題

但是,當我運行我的應用程序時,模板完全被忽略,我只是看到了標準的列表框,而實際上我希望看​​到與「測試」一起的複選框列表。

我試過這幾次,總是相同的結果。我已經在Google上檢查過幾個資源,並且它們都具有用於在ListBox上定義和ItemTemplate的相同類型的語法,所以我確實無法看到我要出錯的地方。

代碼...

<Grid x:Name="LayoutRoot"> 
    <ListBox x:Name="TestList" 
     SelectionMode="Multiple"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <CheckBox Content="Check this checkbox!"/> 
        <TextBlock>Test</TextBlock> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
     <ListBox.Items> 
      <ListBoxItem>Bob</ListBoxItem> 
      <ListBoxItem>Jim</ListBoxItem> 
      <ListBoxItem>Dave</ListBoxItem> 
      <ListBoxItem>Larry</ListBoxItem> 
      <ListBoxItem>Tom</ListBoxItem> 
     </ListBox.Items>    
    </ListBox> 
</Grid> 

任何幫助極大的讚賞。對不起,這樣的啞似乎問題,但我真的在第一障礙這裏:(

AT

回答

17

的ItemTemplate不會工作,當你把一個ListBoxItem直接作爲項目下降。總的概念是你一個數據綁定集合CRL到ListBox.ItemsSource然後指定的ItemTemplate檢查下面的代碼

<Grid x:Name="LayoutRoot"> 
     <ListBox x:Name="TestList" SelectionMode="Multiple"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <CheckBox Content="Check this checkbox!"/> 
         <TextBlock Text="{Binding}"/> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
      <ListBox.Items> 
       <sys:String>Bob</sys:String> 
       <sys:String>Jim</sys:String> 
       <sys:String>Dave</sys:String> 
       <sys:String>Larry</sys:String> 
       <sys:String>Tom</sys:String> 
      </ListBox.Items> 
     </ListBox> 
    </Grid> 

其中SYS是XMLNS:SYS = 「CLR-命名空間:系統;裝配= mscorlib程序」。

以這種方式,有有5個ListBoxItems在th中生成e背景並添加到列表框中。

7

如果要將ListBoxItems直接添加到ListBox,則可以使用ItemContainerStyle而不是ItemTemplate。

但是,僅當您需要每個項目級別的獨特特徵時,纔會這樣做。

如果您計劃使用ItemsSource查看相同的所有項目或製作動態列表,我建議您將字符串(或另一個自定義對象)添加到列表中,並使用ItemTemplate顯示項目。 (見苡樂的答案)

下面是一個使用ItemContainerStyle一個例子:

<ListBox 
     x:Name="TestList" 
     SelectionMode="Multiple"> 

     <ListBox.ItemContainerStyle> 
      <Style 
       TargetType="ListBoxItem"> 

       <Setter 
        Property="Template"> 
        <Setter.Value> 
         <ControlTemplate 
          TargetType="ListBoxItem"> 
          <StackPanel> 
           <CheckBox 
            Content="Check this checkbox!" /> 
           <TextBlock 
            Text="{TemplateBinding Content}" /> 
          </StackPanel> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 

      </Style> 
     </ListBox.ItemContainerStyle> 

     <ListBox.Items> 
      <ListBoxItem>Bob</ListBoxItem> 
      <ListBoxItem>Jim</ListBoxItem> 
      <ListBoxItem>Dave</ListBoxItem> 
      <ListBoxItem>Larry</ListBoxItem> 
      <ListBoxItem>Tom</ListBoxItem> 
     </ListBox.Items> 
    </ListBox> 
0

出於某種原因,如果ListBox中使用的ItemsSource如填充的DataTemplate仍然可以忽略不計:

<ListBox Name="Test" x:FieldModifier="public" ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <TextBox Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

注意,這綁定到包含具有一個屬性的對象(TextAdapter:INotifyPropertyChanged)的ObservableCollection:string Text {...}