0

我有以下内容ItemsControl

<ItemsControl x:Name="ListResult">
   <ItemsControl.ItemTemplate>
      <DataTemplate>
         <DockPanel>
            <Image Margin="10,0,0,0"
                   Source="{Binding Pic}"/>
            <TextBlock Text={Binding Info}/>
         </DockPanel>
      </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>

我将ItemsControl's绑定ItemsSourceList<>.

是否可以为每个项目设置不同的边距?

例如:

ListResult[0].Margin="10,0,0,0";
ListResult[1].Margin="50,0,0,0";
ListResult[2].Margin="10,0,0,0";
ListResult[3].Margin="50,0,0,0";
4

2 回答 2

3

如果您的意思是在行之间交替:来自:WPF:ItemsControl 上的交替颜色? 我将@biju commant 更改为 Margin

<ItemsControl ItemsSource="{Binding ListResult}" AlternationCount="2">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <DockPanel>
                <Image x:Name="image" Source="{Binding Pic}"/>
                <TextBlock Text="{Binding Info}"/>
            </DockPanel>
            <DataTemplate.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Margin" Value="10,0,0,0" TargetName="image"/>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Margin" Value="50,0,0,0" TargetName="image"/>
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
于 2012-12-16T11:55:17.590 回答
-1

是的,您可以在List的元素中设置边距,例如:

ListResult[0].Margin = new Thickness(10, 0, 0, 0); // etc... for rest of elements

然后您的 XAML 代码需要如下所示:

<ItemsControl x:Name="ListResult">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <DockPanel>
                <Image Margin="{Binding Margin}"
                       Source="{Binding Pic}"/>
                <TextBlock Text={Binding Info}/>
           </DockPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
于 2012-12-16T11:48:54.330 回答