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