我在xaml页面中有这段代码:
<ListBox Height="524" HorizontalAlignment="Left" Margin="12,6,0,0" Name="listBox1" VerticalAlignment="Top" Width="438" BorderThickness="1" BorderBrush="White" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Margin="5" TextAlignment="Left" HorizontalAlignment="Left" />
<TextBlock Text="{Binding DeadLine}" Margin="5" TextAlignment="Right" HorizontalAlignment="Right" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
正如您所看到的,我将列表框分成两个文本框,并尝试将一个文本放在最左侧的位置,另一个放在行的最右侧位置。但我不能,这不行。 我错过了什么吗?如何将截止日期对齐?
谢谢, 奥斯卡
答案 0 :(得分:2)
我找到了一个解决方案,混合了我发现的许多解决方案(包括在本论坛中给出)
<ListBox Height="524" HorizontalAlignment="Left" Margin="12,6,0,0" Name="listBox1" VerticalAlignment="Top" Width="438" BorderThickness="1" BorderBrush="White" >
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding DeadLine}" HorizontalAlignment="Right"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
TKS!
答案 1 :(得分:0)
您需要确保ItemContainerStyle
有HorizontalContentAlignment
来拉伸。
这对我有用:
<ListBox Height="524" HorizontalAlignment="Left" Margin="12,6,0,0" Name="listBox1" VerticalAlignment="Top" Width="438" BorderThickness="1" BorderBrush="White" >
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Stretch" Orientation="Horizontal" Background="Red">
<TextBlock Text="{Binding Name}" Margin="5" TextAlignment="Left" HorizontalAlignment="Left" />
<TextBlock Text="{Binding DeadLine}" Margin="5" TextAlignment="Right" HorizontalAlignment="Right" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>