DatatemplateSelector,Listbox stretch

时间:2012-09-05 22:27:51

标签: c# silverlight xaml listbox stretch

我在ListBox中使用DatatemplateSelector。

我的数据不会根据需要拉伸(灰色圆圈必须位于列表框的右侧)

<ListBox HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" SelectionChanged="ListBoxMessagesSelectionChanged" x:Name="listmy" >
    <ListBox.ItemTemplate>
      <DataTemplate>
        <local:SelectorForSearchFriend Content="{Binding}">
          <local:SelectorForSearchFriend.User>
            <DataTemplate>
              <Grid HorizontalAlignment="Stretch">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="3*"/>
                  <ColumnDefinition Width="15*"/>
                  <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <Image Grid.Column="0" Width="100" Height="70"/>
                <TextBlock Name="Title" Text="{Binding Title}"/>
                <Ellipse HorizontalAlignment="Right" Fill="#FFB8B8B8" Height="20" Width="20" StrokeThickness="0" Visibility="{Binding IsOnline}" Grid.Column="2" Margin="0,-20,0,0"/>
              </Grid>
            </DataTemplate>
          </local:SelectorForSearchFriend.User>
      </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemContainerStyle>
      <Style TargetType="ListBoxItem">
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="Margin" Value="0,4,0,4"/>
      </Style>
    </ListBox.ItemContainerStyle>
  </ListBox>

screenshot

1 个答案:

答案 0 :(得分:7)

  <phone:PhoneApplicationPage.Resources>
    <Style TargetType="Grid">
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
    </Style>
    <Style TargetType="local:SelectorForSearchFriend">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:SelectorForSearchFriend">
                    <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="VerticalAlignment" Value="Stretch"/>
    </Style>
  </phone:PhoneApplicationPage.Resources>