2

I have a list box which is of a certain fixed width. The number of items in the listbox varies. Is there a way to center the contents of the list box? The "Content Presenter" of the ListBoxItem ,centers each item inside its Template instead of centering it with respect to the entire listbox width.

Sorry about not replying earlier. The issue was with the width of the ItemsPanelTemplate which I was using in my Listbox. Earlier Width was set to 925. Changing this Width to MaxWidth worked. The code:

<ItemsPanelTemplate x:Key="ItemsPanelKey">
        <Contact:AnimatedWrapPanel HorizontalAlignment="Center" MaxWidth="925">
           <Contact:AnimatedWrapPanel.Interpolation>
                <interpolate:BackInterpolation Amplitude=".5" Suppression=".2" EdgeBehavior="5"/>
            </Contact:AnimatedWrapPanel.Interpolation>
        </Contact:AnimatedWrapPanel>

   </ItemsPanelTemplate>
4

1 回答 1

0

不确定,但听起来您想要的是一个以每个项目为中心的自定义项目模板。如果我是对的,唯一棘手的是模板必须与列表框具有相同的固定宽度。因此,如果您的列表框包含一些对象 Foo 并且 Foo.Text 是要显示的简单文本属性,您可以在 Xaml 中这样做:

  <ListBox x:Name="TheListBox" Width="300">
    <ListBox.ItemTemplate>
      <DataTemplate>
        <Grid Width="300">
          <TextBlock Text="{Binding Text}" HorizontalAlignment="Center" />
        </Grid>
      </DataTemplate>
    </ListBox.ItemTemplate>
  </ListBox>

后面的代码包含如下内容:

List<Foo> ListOfFoo = new List<Foo>();
ListOfFoo.Add(new Foo(){Text="Something"});
ListOfFoo.Add(new Foo(){Text="Something Else"});
ListOfFoo.Add(new Foo(){Text="Something Completely Different"});

TheListBox.ItemsSource = ListOfFoo;

如果它更复杂或者您无法使其工作,请发布一些代码,我们将从那里开始。

于 2009-08-13T02:40:46.687 回答