简体   繁体   中英

WPF - TextBlocks in ListView are not horizontal aligned

I have made a ListView in a WPF-application with different TextBlocks. Each row can contain several TextBlocks, but when a row contains more than one text block, the first one fits to the row, but the following TextBlocks does not align horizontally (see picture). I have no idea what could be the cause of this, so I hope someone could provide some clarification.

在此输入图像描述

Below is the XAML-code.

<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>

EDIT: The problem was solved when the ListView was set with to include a Stackpanel with Orientation="Horizontal".

I think if you add a style for a listview item to the item container style your problem gets fixed. If not just move the setter to your list box item style.

<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>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>
</Grid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM