如何显示交替对齐的行? [英] How can I display rows with alternating aligning?
问题描述
我想用这张图片实现类似的效果:
交替对齐的行 - 当行索引为偶数时向左对齐,当行索引不均匀时向右对齐.
我尝试使用ListBox
的ListBox
,绑定ObservableCollection
.是否应该绑定第二个ListBox
的Margin
属性来实现交替对齐?
<ListBox.ItemTemplate><数据模板><网格><列表框名称=内部列表框"HorizontalAlignment="{Binding HorizontalAlignment}";背景=透明"BorderThickness=0"ItemContainerStyle="{StaticResource ChangeListViewItemHighlight}";ItemsSource="{Binding}";PreviewKeyDown=InnerListBox_PreviewKeyDown"><ListBox.ItemTemplate><数据模板><椭圆宽度={装订尺寸}"高度={装订尺寸}"光标=手"填充={装订背景}"/></数据模板></ListBox.ItemTemplate></列表框></网格></数据模板></ListBox.ItemTemplate></列表框>
关于您的要求,我从您提供的示例中假设:
- 有多行.
- 每一行都有相同数量的项目(圆圈,这里是 5 个).
- 所有项目(圆圈)的大小都相同.
通过这种方式,您可以通过几个步骤使用内置功能实现所需的行为.
- 更改内部
ListBox
中的ItemsPanel
,以水平(按行)显示项目(圆圈) - 设置
<块引用>附加问题:如何去除所选行的突出显示?
在这种简单的情况下,您可以使用
ItemsControl
作为外部控件而不是ListBox
.它没有选择.但是,您必须设置ContentPresenter
的样式,然后使用Margin
.<ItemsControl.ItemContainerStyle><Style TargetType="{x:Type ContentPresenter}"><样式.资源><AlternationConverter x:Key=AlternationPaddingConverter"><厚度右=25"/><左厚度=25"/></AlternationConverter></Style.Resources><Setter 属性 =保证金"值={Binding(ItemsControl.AlternationIndex),RelativeSource={RelativeSource Self},Converter={StaticResource AlternationPaddingConverter}}"/></风格></ItemsControl.ItemContainerStyle><ItemsControl.ItemTemplate><数据模板><网格><列表框名称=内部列表框"背景=透明"BorderThickness=0"ItemContainerStyle="{StaticResource ChangeListViewItemHighlight}";ItemsSource="{Binding}";PreviewKeyDown=InnerListBox_PreviewKeyDown"><ListBox.ItemsPanel><ItemsPanelTemplate><VirtualizingStackPanel Orientation=水平"/></ItemsPanelTemplate></ListBox.ItemsPanel><ListBox.ItemTemplate><数据模板><椭圆宽度={装订尺寸}"高度={装订尺寸}"光标=手"填充={装订背景}"/></数据模板></ListBox.ItemTemplate></列表框></网格></数据模板></ItemsControl.ItemTemplate></ItemsControl> I want to achieve something similar with this image:
Rows with alternating alignment - aligned to the left when the row's index is even, aligned to the right when the row's index is uneven.
I tried to use
ListBox
ofListBox
, binding withObservableCollection<ObservableCollection<Circle>>
. Should I bind theMargin
property of the secondListBox
in order to achieve the alternating alignment?<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>
解决方案Regarding your requirements, I assume from the example you provided that:
- There are multiple rows.
- Each row has the same number of items (circles, here 5).
- All items (circles) have the same size.
This way, you can achieve the desired behavior with built-in functionality in a few steps.
- Change the
ItemsPanel
in the innerListBox
to display the items (circles) horizontally (as a row) - Set an
AlternationCount
of2
to the outerListBox
, so there are two alternation indices. - Create an item container style for the out
ListBox
, which sets aPadding
(orMargin
if you want) of half the width of your circle items on the left forAlternationIndex
0
and the same on the right otherwise. You can create an instance of the already existingAlternationConverter
to define thePadding
s.
In this example, the paddings are fixed size (for me circle have a size of
50
, so the padding is25
). You could also create a custom value converter to make this more dynamic, binding the realSize
.<ListBox Name="OuterListBox" AlternationCount="2" HorizontalAlignment="Center" VerticalAlignment="Center" Background="White" BorderThickness="0" ItemsSource="{Binding Board}"> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}"> <Style.Resources> <AlternationConverter x:Key="AlternationPaddingConverter"> <Thickness Right="25"/> <Thickness Left="25"/> </AlternationConverter> </Style.Resources> <Setter Property="Padding" Value="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource Self}, Converter={StaticResource AlternationPaddingConverter}}"/> </Style> </ListBox.ItemContainerStyle> <ListBox.ItemTemplate> <DataTemplate> <Grid> <ListBox Name="InnerListBox" Background="Transparent" BorderThickness="0" ItemContainerStyle="{StaticResource ChangeListViewItemHighlight}" ItemsSource="{Binding}" PreviewKeyDown="InnerListBox_PreviewKeyDown"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBox.ItemTemplate> <DataTemplate> <Ellipse Width="{Binding Size}" Height="{Binding Size}" Cursor="Hand" Fill="{Binding Background}" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
Additional question: how can I remove the highlight of the selected line?
In this simple case, you could use an
ItemsControl
as outer control instead of aListBox
. It has no selection. However, you have to style theContentPresenter
and useMargin
then.<ItemsControl Name="OuterListBox" AlternationCount="2" HorizontalAlignment="Center" VerticalAlignment="Center" Background="White" BorderThickness="0" ItemsSource="{Binding Board}"> <ItemsControl.ItemContainerStyle> <Style TargetType="{x:Type ContentPresenter}"> <Style.Resources> <AlternationConverter x:Key="AlternationPaddingConverter"> <Thickness Right="25"/> <Thickness Left="25"/> </AlternationConverter> </Style.Resources> <Setter Property="Margin" Value="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource Self}, Converter={StaticResource AlternationPaddingConverter}}"/> </Style> </ItemsControl.ItemContainerStyle> <ItemsControl.ItemTemplate> <DataTemplate> <Grid> <ListBox Name="InnerListBox" Background="Transparent" BorderThickness="0" ItemContainerStyle="{StaticResource ChangeListViewItemHighlight}" ItemsSource="{Binding}" PreviewKeyDown="InnerListBox_PreviewKeyDown"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBox.ItemTemplate> <DataTemplate> <Ellipse Width="{Binding Size}" Height="{Binding Size}" Cursor="Hand" Fill="{Binding Background}" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>
这篇关于如何显示交替对齐的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!