gpt4 book ai didi

.net - 对齐 ListBoxItem 内容

转载 作者:行者123 更新时间:2023-12-01 11:06:06 26 4
gpt4 key购买 nike

我正在尝试对齐 ListBoxItem 的内容。在下面的示例中,我想让 TextBlock 左对齐,Button 在 ListBox 的每一行中右对齐。但是 Button 总是紧跟在 TextBlock 文本的末尾之后,并且在 ListBox 中没有右对齐。

<StackPanel>
<ListBox ItemsSource="{Binding MyDataList}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding SomeTextProperty}"
VerticalAlignment="Center" Margin="5" />
<Button Content="Display"
HorizontalAlignment="Right" Margin="5"
Command="{Binding SomeCommand}"
CommandParameter="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>

我想我的 XAML 中需要更改一些内容。我做错了什么?

感谢您的帮助!

最佳答案

您需要不同的面板类型,但您还需要让内容延伸到 ListBox 中。您可以将其指定为 ListBoxItem 的 ControlTemplate,或者使用 DataTemplate 并将 ListBox Horizo​​ntalContentAlignment 设置为拉伸(stretch)(在 Dan Bryant 在问题下的评论中+1 以指出这一点)。

<ListBox>
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<TextBlock Text="{Binding SomeTextProperty}" VerticalAlignment="Center" Margin="5"/>
<Button Content="Display" HorizontalAlignment="Right" Margin="5"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
</ListBox>

        <ListBox HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<TextBlock Text="{Binding SomeTextProperty}" VerticalAlignment="Center" Margin="5"/>
<Button Content="Display" HorizontalAlignment="Right" Margin="5"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

关于.net - 对齐 ListBoxItem 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5745031/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com