如何显示交替对齐的行? [英] How can I display rows with alternating aligning?

查看:30
本文介绍了如何显示交替对齐的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用这张图片实现类似的效果:

交替对齐的行 - 当行索引为偶数时向左对齐,当行索引不均匀时向右对齐.

我尝试使用ListBoxListBox,绑定ObservableCollection>.是否应该绑定第二个ListBoxMargin属性来实现交替对齐?

<ListBox.ItemTemplate><数据模板><网格><列表框名称=内部列表框"Horizo​​ntalAlignment="{Binding Horizo​​ntalAlignment}";背景=透明"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 of ListBox, binding with ObservableCollection<ObservableCollection<Circle>>. Should I bind the Margin property of the second ListBox 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 inner ListBox to display the items (circles) horizontally (as a row)
    • Set an AlternationCount of 2 to the outer ListBox, so there are two alternation indices.
    • Create an item container style for the out ListBox, which sets a Padding (or Margin if you want) of half the width of your circle items on the left for AlternationIndex 0 and the same on the right otherwise. You can create an instance of the already existing AlternationConverter to define the Paddings.

    In this example, the paddings are fixed size (for me circle have a size of 50, so the padding is 25). You could also create a custom value converter to make this more dynamic, binding the real Size.

    <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 a ListBox. It has no selection. However, you have to style the ContentPresenter and use Margin 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆