我有以下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
的{{1}}绑定到ItemsSource
。
是否可以为每个项目设置不同的保证金?
例如:
List<>
答案 0 :(得分:3)
如果您的意思是在行之间交替: 来自:WPF: Alternating colors on a ItemsControl? 我将@biju命令更改为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>
答案 1 :(得分:-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>