WPF - ListView中的TextBlocks不是水平对齐的

时间:2017-05-12 07:59:44

标签: c# wpf xaml

我在具有不同TextBlocks的WPF应用程序中创建了一个ListView。每行可以包含多个TextBlock,但是当一行包含多个文本块时,第一行适合该行,但下面的TextBlocks不会水平对齐(参见图片)。我不知道造成这种情况的原因是什么,所以我希望有人可以提供一些澄清。

enter image description here

以下是XAML代码。

<ListView ItemsSource="{Binding DataContext.Mechanics, RelativeSource={RelativeSource AncestorType={x:Type Window}}, UpdateSourceTrigger=PropertyChanged}" Height="auto" Width="1330" 
             Grid.Row="2" Grid.ColumnSpan="13" BorderThickness="0" Background="#FFF2F2F2">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="18"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="22"/>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="1200"/>
                    </Grid.ColumnDefinitions>
                    <Label Content="{Binding MecID}" Grid.Column="0" Padding="0" FontSize="11" Background="#FFF2F2F2"/>
                    <Label Content="{Binding Name}" Grid.Column="1" Padding="0" FontSize="11" Margin="2, 0, 0, 0"/>
                    <ListView ItemsSource="{Binding MecJobs, UpdateSourceTrigger=PropertyChanged}" BorderThickness="0" Padding="-1" Height="18" Width="1200" Grid.Column="2" ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                             ScrollViewer.VerticalScrollBarVisibility="Disabled" Background="White">
                        <ListView.ItemContainerStyle>
                            <Style TargetType="ListBoxItem">
                                <Setter Property="Padding" Value="0"/>
                            </Style>
                        </ListView.ItemContainerStyle>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Canvas>
                                    <ListViewItem Width="{Binding EstimatedTimeWidth}" Canvas.Left="{Binding Margin}" Background="{Binding Color}" Height="18" 
                                               Padding="0" Margin="0" ScrollViewer.VerticalScrollBarVisibility="Disabled">
                                        <TextBlock TextAlignment="Center" Height="18" Padding="0" Margin="0">
                                            <TextBlock.Text>
                                                <MultiBinding StringFormat="{}{0} - {1}">
                                                    <Binding Path="RegNumber" />
                                                    <Binding Path="CustomerName" />
                                                </MultiBinding>
                                            </TextBlock.Text>
                                        </TextBlock>
                                    </ListViewItem>
                                </Canvas>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ListView>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
        <ListView.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="Padding" Value="0"/>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>
</Grid>

编辑:当ListView设置为包含一个方向=&#34;水平&#34;的Stackpanel时,问题解决了。

0 个答案:

没有答案