2012-02-18 43 views
0

喜,列表項模板

所以讓我們假設我有這個ListBox.ItemTemplate:

<phone:PhoneApplicationPage.Resources> 
    <DataTemplate x:Key="DataTemplate1"> 
     <StackPanel Orientation="Horizontal"> 
      <StackPanel Height="100" Width="100"> 
       <TextBlock TextWrapping="Wrap" Text="{Binding name}" FontWeight="Bold"/> 
       <TextBlock TextWrapping="Wrap" Text="{Binding age}" FontWeight="Bold"/> 
      </StackPanel> 
      <StackPanel Height="100" Width="100"> 
       <Image Height="100"/> 
       <Image Height="100"/> 
      </StackPanel> 
      <StackPanel Height="100" Width="100"> 
       <TextBlock TextWrapping="Wrap" Text="blah blah" FontSize="13.333"/> 
       <TextBlock TextWrapping="Wrap" Text="{Binding something}"/> 
      </StackPanel> 
      <StackPanel Height="100" Width="100"> 
       <TextBlock TextWrapping="Wrap" Text="Time" FontSize="13.333"/> 
       <TextBlock TextWrapping="Wrap" Text="45 minutes"/> 
      </StackPanel> 
     </StackPanel> 
    </DataTemplate> 
</phone:PhoneApplicationPage.Resources> 

我想實現的是,不知何故在第二圖像的數StackPanel中,這一個:

   <StackPanel Height="100" Width="100"> 
       <Image Height="100"/> 
       <Image Height="100"/> 
      </StackPanel> 

是動態的,兩個用於某些列表框中的項目,3或4人。

我想知道是否有可能實現這與綁定和模板? 我不想在代碼中手動執行此操作。

回答

2

您可以用ListBox替換該特定的StackPanel。然後可以將ListBox綁定到Image-collection,並可以將其ItemTemplate設置爲顯示圖像。就像這樣:

 <DataTemplate x:Key="DataTemplate1"> 
     <StackPanel Orientation="Horizontal"> 
      <StackPanel Height="100" Width="100"> 
       <TextBlock TextWrapping="Wrap" Text="{Binding name}" FontWeight="Bold"/> 
       <TextBlock TextWrapping="Wrap" Text="{Binding age}" FontWeight="Bold"/> 
      </StackPanel> 
      <ListBox ItemsSource="{Binding DynamicCollectionOfImages}"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <Image Height="100" Source="{Binding .}"/> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
      <StackPanel Height="100" Width="100"> 
       <Image Height="100"/> 
       <Image Height="100"/> 
      </StackPanel> 
      <StackPanel Height="100" Width="100"> 
       <TextBlock TextWrapping="Wrap" Text="blah blah" FontSize="13.333"/> 
       <TextBlock TextWrapping="Wrap" Text="{Binding something}"/> 
      </StackPanel> 
      <StackPanel Height="100" Width="100"> 
       <TextBlock TextWrapping="Wrap" Text="Time" FontSize="13.333"/> 
       <TextBlock TextWrapping="Wrap" Text="45 minutes"/> 
      </StackPanel> 
     </StackPanel> 
    </DataTemplate> 
-1

在有你應該添加主內彼此列表框,把Image控件作爲一個ItemTemplate每個列表項不同的一組圖像。

但是,當你只有一些圖像集(例如2,3和4個靜態圖像)的整個列表,並希望顯示其中的每一個列表項,你可以準備3 StackPanel的listBoxItem模板並更改它可見性取決於DataSource的一些屬性。該屬性值應該轉換爲Visibility枚舉成員。

例如,當圖像應該依賴於整數ImagesSet屬性從數據源:

<StackPanel Height="100" Width="100" Visibility={Binding ImagesSet, Converter={StaticResources ImagesSetToVisibility}, ConverterParameter=2}> 
    <Image Height="100"/> 
    <Image Height="100"/> 
</StackPanel> 
<StackPanel Height="100" Width="100" Visibility={Binding ImagesSet, Converter={StaticResources ImagesSetToVisibility}, ConverterParameter=3}> 
    <Image Height="100"/> 
    <Image Height="100"/> 
    <Image Height="100"/> 
</StackPanel> 
<StackPanel Height="100" Width="100" Visibility={Binding ImagesSet, Converter={StaticResources ImagesSetToVisibility}, ConverterParameter=4}> 
    <Image Height="100"/> 
    <Image Height="100"/> 
    <Image Height="100"/> 
    <Image Height="100"/> 
</StackPanel> 

該轉換器應該檢查,如果值等於參數和返回Visibility.Visbile或Visibility.Collapsed:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
{ 
    return ((int)value) == ((int)parameter) ? Visibility.Visible : Visibility.Collapsed; 
}