在数据模板中将列表框的左侧元素和右侧元素对齐

问题描述 投票:0回答:1

我有一个字符串数组。 我需要创建一个列表框,在左侧显示这些字符串,在右侧显示它们的长度。所有这些,四个项目上有交替的颜色,如果尺寸是奇数,则必须用斜体书写。 除了将字符串大小向右对齐之外,我设法完成所有操作。

这是管理列表框的 XAML 部分

 <Window.Resources>
     <local:ItalicConverter x:Key="ItalicConverter"/>
     <Style TargetType="{x:Type ListBoxItem}">
         <Style.Triggers>
             <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                 <Setter Property="Background" Value="LightBlue"/>
             </Trigger>
             <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                 <Setter Property="Background" Value="CornflowerBlue"/>
             </Trigger>
             <Trigger Property="ItemsControl.AlternationIndex" Value="2">
                 <Setter Property="Background" Value="DarkBlue"/>
             </Trigger>
             <Trigger Property="ItemsControl.AlternationIndex" Value="3">
                 <Setter Property="Background" Value="CornflowerBlue"/>
             </Trigger>
         </Style.Triggers>
     </Style>
 </Window.Resources>
 <ListBox x:Name="listBox" AlternationCount="4" HorizontalAlignment="Stretch">
     <ListBox.ItemTemplate>
         <DataTemplate>
             <Grid HorizontalAlignment="Stretch">
                 <TextBlock Text="{Binding}" Foreground="White" Margin="5" HorizontalAlignment="Left">
                     <TextBlock.FontStyle>
                         <Binding Path="Length" Converter="{StaticResource ItalicConverter}"/>
                     </TextBlock.FontStyle>
                 </TextBlock>
                 <TextBlock Text="{Binding Length}" Foreground="White" Margin="5" HorizontalAlignment="Right"/>
             </Grid>
         </DataTemplate>
     </ListBox.ItemTemplate>
 </ListBox>

我明白了: resultat1

我也尝试过这个:

<Window x:Class="Ex1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Ex1"
        mc:Ignorable="d"
        Title="Voeux de cours InteGraph" Height="450" Width="800">
    <Window.Resources>
        <local:ItalicConverter x:Key="ItalicConverter"/>
        <Style TargetType="{x:Type ListBoxItem}">
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Background" Value="LightBlue"/>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="CornflowerBlue"/>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="2">
                    <Setter Property="Background" Value="DarkBlue"/>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="3">
                    <Setter Property="Background" Value="CornflowerBlue"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <ListBox x:Name="listBox" AlternationCount="4" HorizontalAlignment="Stretch">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid HorizontalAlignment="Stretch">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding}" Foreground="White" Margin="5" HorizontalAlignment="Left" Grid.Column="0">
                        <TextBlock.FontStyle>
                            <Binding Path="Length" Converter="{StaticResource ItalicConverter}"/>
                        </TextBlock.FontStyle>
                    </TextBlock>
                    <TextBlock Text="{Binding Length}" Foreground="White" Margin="5" HorizontalAlignment="Right" Grid.Column="2"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Window>

但是结果并不好... resultat2

在 stackOverflow 上,我找到了这个链接:lien stackOverflow

但他们的答案都不适合我。

wpf listbox
1个回答
0
投票

您只是缺少一个组件。您的

ListBoxItem
需要伸展:

请参阅此链接:如何获取 ListBox ItemTemplate 以水平拉伸 ListBox 的整个宽度?

<ListBox.ItemContainerStyle> 
    <Style TargetType="ListBoxItem"> 
        <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter> 
    </Style> 
</ListBox.ItemContainerStyle> 
© www.soinside.com 2019 - 2024. All rights reserved.