2011-04-21 98 views
3

我想對齊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中有些東西需要改變。我究竟做錯了什麼?

感謝您的幫助!

+2

只有幾分鐘或我會後的代碼,但是你可以把一個DockPanel中,而不是一個StackPanel的項目(使用碼頭分別向左和右),然後設置Horizo​​ntalContentAlignment在列表框上拉伸。 – 2011-04-21 13:54:36

+0

@丹·布萊恩特:感謝您的指導。 DockPanel解決方案也可以正常工作。我只注意到我必須定義Button的固定寬度,否則每個Button從TextBlock文本的末尾延伸到ListBox的右邊框。 – Slauma 2011-04-21 14:58:45

+1

以供將來參考,DockPanel的屬性LastChildFill默認爲True,這會導致您觀察到的行爲。如果您將此屬性設置爲false,則最後一個孩子將按照指示進行對接。 – 2011-04-21 23:01:52

回答

3

您需要不同的面板類型,但您還需要獲取內容列表框。你可以將它指定爲ListBoxItem的ControlTemplate,或者使用DataTemplate並設置ListBox的Horizo​​ntalContentAlignment來伸展(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> 
+0

我選擇了第二個最簡單的解決方案。 「Grid」上的Horizo​​ntalAlignment =「Stretch」並不是必需的。謝謝! – Slauma 2011-04-21 14:50:56

1

嘗試使用其他面板而不是堆疊面板。網格或底座應該做的工作相當不錯。

1

只是任何值替換您的網格寬度:

<ListBox ItemsSource="{Binding MyDataList}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid Width="150"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="auto"/> 
        <ColumnDefinition Width="*"/> 
        <ColumnDefinition Width="auto"/> 
       </Grid.ColumnDefinitions> 
       <TextBlock Grid.Column="0" 
          Text="{Binding BusinessProperty}"       
          VerticalAlignment="Center" 
          Margin="5" /> 
       <Button Grid.Column="1" 
         Content="Display"       
         HorizontalAlignment="Right" Margin="5"       Command="{Binding SomeCommand}"       CommandParameter="{Binding}"/> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

的texblock和按鈕將採取只是他們需要的地方,中間的格欄,將填補剩餘的空間,「推」第一,最後一列在網格提供的空間量(這裏是150px)左側和右側分別爲

+0

好的解決方案,但有一個令人不安的地方:我不想指定一個固定的網格寬度;它應該與ListBox的寬度一致。 Mark Synowiec的答案中的第二個解決方案正是如此。但無論如何,感謝您的幫助! – Slauma 2011-04-21 14:53:53