【发布时间】:2021-04-21 19:55:44
【问题描述】:
我想用这张图片实现类似的效果:
交替对齐的行 - 行索引为偶数时左对齐,行索引不偶时右对齐。
我尝试使用ListBox 的ListBox,与ObservableCollection<ObservableCollection<Circle>> 绑定。我应该绑定第二个ListBox 的Margin 属性以实现交替对齐吗?
<ListBox
Name="OuterListBox"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="White"
BorderThickness="0"
ItemsSource="{Binding Board}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<ListBox
Name="InnerListBox"
HorizontalAlignment="{Binding HorizontalAlignment}"
Background="Transparent"
BorderThickness="0"
ItemContainerStyle="{StaticResource ChangeListViewItemHighlight}"
ItemsSource="{Binding}"
PreviewKeyDown="InnerListBox_PreviewKeyDown">
<ListBox.ItemTemplate>
<DataTemplate>
<Ellipse
Width="{Binding Size}"
Height="{Binding Size}"
Cursor="Hand"
Fill="{Binding Background}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
【问题讨论】:
-
通过格式化 ListBox.Items 中的对象的方式可能更容易做到这一点。也许在交替行的开头插入 1/2 宽度的东西?
-
行数是否固定?
-
@james.lee 是的,列数也是固定的
-
@TerryTyson 没用。
标签: c# wpf xaml data-binding