3

我在 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>

截屏

4

1 回答 1

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>
于 2012-09-06T03:41:09.973 回答