我想對齊ListBoxItem
的內容。在下面的示例中,我希望TextBlock
左對齊,並且Button
右對齊列表框的每一行中。但Button總是在TextBlock的文本結束後緊跟其後,並且不在ListBox中對齊。對齊ListBoxItem內容
<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中有些東西需要改變。我究竟做錯了什麼?
感謝您的幫助!
只有幾分鐘或我會後的代碼,但是你可以把一個DockPanel中,而不是一個StackPanel的項目(使用碼頭分別向左和右),然後設置HorizontalContentAlignment在列表框上拉伸。 – 2011-04-21 13:54:36
@丹·布萊恩特:感謝您的指導。 DockPanel解決方案也可以正常工作。我只注意到我必須定義Button的固定寬度,否則每個Button從TextBlock文本的末尾延伸到ListBox的右邊框。 – Slauma 2011-04-21 14:58:45
以供將來參考,DockPanel的屬性LastChildFill默認爲True,這會導致您觀察到的行爲。如果您將此屬性設置爲false,則最後一個孩子將按照指示進行對接。 – 2011-04-21 23:01:52