ListBox 布局,如何每行包括两个并排控件,宽度不同?
listbox的每行显示需要量个控件,即TextBlock和button,textblock在最左边左边,button在最右边显示,textblock的width长,button的内容为">"如下图:该怎样布局我总是调不好?
我的xmal:
<ListBox x:Name="MainLongListSelector" Margin="0,0,10,0" ItemsSource="{Binding persons}">--------------------编程问答-------------------- 指定button的width属性。 --------------------编程问答-------------------- 首先,你把那个StackPanel去掉比较好
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation ="Horizontal">
<Grid>
<TextBlock Text="{Binding DisplayName}"
Foreground="Black"
HorizontalAlignment="Left"
TextTrimming="WordEllipsis"
Margin="30,10,20,10"
VerticalAlignment="Center"
Style="{StaticResource PhoneTextLargeStyle}"/>
<Button x:Name="Detail"
HorizontalAlignment="Right" BorderThickness="0"
Margin="0,10,0,10"
VerticalAlignment="Bottom"
Click="Detail_Click">
<Button.Background>
<ImageBrush Stretch="Uniform" ImageSource="/Images/rightwards.png"/>
</Button.Background>
</Button>
</Grid>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
然后,给Grid设置两个列,把TextBlock放第一列,把Button放第二列。 --------------------编程问答--------------------
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
这个样子是没有作用的,由于考虑的各种手机的屏幕大小,所以并不想给button或textblock一个固定的width --------------------编程问答-------------------- <ListBox x:Name="MainLongListSelector"
Margin="0,0,10,0"
ItemsSource="{Binding person}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding DisplayName}"
Foreground="Black"
HorizontalAlignment="Left"
TextTrimming="WordEllipsis"
Margin="30,10,20,10"
VerticalAlignment="Center"
Style="{StaticResource PhoneTextLargeStyle}" />
<Button x:Name="Detail"
HorizontalAlignment="Right"
BorderThickness="0"
Margin="0,10,0,10"
VerticalAlignment="Bottom"
Click="Detail_Click">
<Button.Background>
<ImageBrush Stretch="Uniform"
ImageSource="/Images/rightwards.png" />
</Button.Background>
</Button>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox> --------------------编程问答-------------------- listbox的item默认是左对齐,而不是拉伸充满整个父容器的可用宽度,需要设置下ItemContainerStyle更改下。
你可以考虑用LongListSelector,它的item默认是拉伸填充整个父容器的可用宽度的 --------------------编程问答--------------------
谢谢您的回答,Longlistseletor 删除有bug,从头依次删除时,条目大于5或者10,会发生异常,倒着删除没有问题,这是控件的bug,我没办法才用的listbox
补充:移动开发 , Windows Phone