2

有没有更好的方法来对齐列?当文本更改时,第 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>   
4

1 回答 1

4

您可以尝试在 ListBox 上设置Grid.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>   
于 2013-05-14T19:00:04.267 回答