列对齐在列表框

本文关键字:列表 对齐 | 更新日期: 2023-09-27 18:13:48

是否有更好的方法来对齐列?当文本改变时,第3列和第4列到处移动。我试过将宽度设置为*和自动,但似乎没有任何作用。

<ListBox Height="Auto" BorderThickness="0" MouseDoubleClick="OnMouseDoubleClick" ItemsSource="{Binding Path=Results}" SelectedItem="{Binding Path=Entry, Mode=TwoWay}" AlternationCount="2">              
   <ListBox.ItemTemplate>
      <DataTemplate>
         <Grid>
            <Grid.ColumnDefinitions>
               <ColumnDefinition MinWidth="150"></ColumnDefinition>
               <ColumnDefinition MinWidth="150"></ColumnDefinition>
               <ColumnDefinition MinWidth="150"></ColumnDefinition>
               <ColumnDefinition MinWidth="100"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" FontFamily="Sagoe UI" FontSize="14" 
                       Text="{Binding FullName}" Padding="2"  />
            <TextBlock Grid.Column="1" FontFamily="Sagoe UI" FontSize="14" 
                       Text="{Binding Company}" Padding="2" />
            <TextBlock Grid.Column="2" FontFamily="Sagoe UI" FontSize="14" 
                       Text="{Binding BusinessPhone}" Padding="2" />
            <TextBlock Grid.Column="3" FontFamily="Sagoe UI" FontSize="14" 
                       Text="{Binding EmailAddress}" Padding="2" >  
            </TextBlock>
         </Grid>                        
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>   

列对齐在列表框

您可以尝试设置Grid。在ListBox上设置IsSharedSizeScope,并在每个网格列上设置唯一的SharedSizeGroup。这应该对齐你的列。

<ListBox Grid.IsSharedSizeScope="True" Height="Auto" BorderThickness="0" MouseDoubleClick="OnMouseDoubleClick" ItemsSource="{Binding Path=Results}" SelectedItem="{Binding Path=Entry, Mode=TwoWay}" AlternationCount="2">              
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="A"></ColumnDefinition>
                            <ColumnDefinition SharedSizeGroup="B"></ColumnDefinition>
                            <ColumnDefinition SharedSizeGroup="C"></ColumnDefinition>
                            <ColumnDefinition SharedSizeGroup="D"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" FontFamily="Sagoe UI" FontSize="14" Text="{Binding FullName}" Padding="2"  />
                        <TextBlock Grid.Column="1" FontFamily="Sagoe UI" FontSize="14" Text="{Binding Company}" Padding="2" />
                        <TextBlock Grid.Column="2" FontFamily="Sagoe UI" FontSize="14" Text="{Binding BusinessPhone}" Padding="2" />
                        <TextBlock Grid.Column="3" FontFamily="Sagoe UI" FontSize="14" Text="{Binding EmailAddress}" Padding="2" >  
                        </TextBlock>
                    </Grid>                        
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>