0

I have the following 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>

I bind the ItemsControl's ItemsSource to a List<>.

Is it possible to put a different margin for each item?

For example:

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";
tshepang
  • 12,111
  • 21
  • 91
  • 136
Reyn
  • 269
  • 5
  • 17

2 Answers2

3

If you mean to alternate between lines : from : WPF: Alternating colors on a ItemsControl? I changed @biju commant to 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>
Community
  • 1
  • 1
ZSH
  • 905
  • 5
  • 15
-1

Yes, you can set margins in your List's elements like:

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

And then your XAML code needs to be like:

<ItemsControl x:Name="ListResult">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <DockPanel>
                <Image Margin="{Binding Margin}"
                       Source="{Binding Pic}"/>
                <TextBlock Text={Binding Info}/>
           </DockPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
EugenSoft
  • 536
  • 4
  • 13