2012-02-18 75 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; 
}