如何在listview中拉伸listviewitem
how to stretch listviewitem in listview
开发人员。
我尝试在水平方向上拉伸列表视图项目。
我确实重置了 ItemContainerStyle ,并且水平对齐是 strech。
这是 MainPage.xaml.
中的所有代码
<Grid x:Name="rootGrid"
HorizontalAlignment="Stretch"
MinWidth="320"
MaxWidth="480"
Width="Auto">
<ListView x:Name="infoFlowListView"
MinWidth="320"
MaxWidth="480"
Width="Auto"
HorizontalAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<local:MyUserControl1 MinWidth="300" HorizontalAlignment="Stretch"></local:MyUserControl1>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Grid>
这里是MainPage.xaml.cs中的所有代码
public MainPage()
{
var items = new List<MyUserControl1>();
for (int i = 0; i < 50; i++)
{
items.Add(new MyUserControl1());
}
this.InitializeComponent();
infoFlowListView.ItemsSource = items;
}
这是MyUserControl.xaml
中的所有代码
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="4*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="8"></RowDefinition>
</Grid.RowDefinitions>
<Ellipse Width="60" Height="60"
Grid.Column="0" Grid.Row="0" Grid.RowSpan="3"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Stretch="Uniform">
<Ellipse.Fill>
<ImageBrush ImageSource="Assets/1.png"></ImageBrush>
</Ellipse.Fill>
</Ellipse>
<RelativePanel x:Name="topRelativePanel"
Grid.Column="1" Grid.Row="0"
HorizontalAlignment="Stretch">
<TextBlock Text="TestName"
VerticalAlignment="Bottom"
RelativePanel.AlignLeftWithPanel="True"
FontSize="12"
HorizontalAlignment="Stretch"></TextBlock>
<TextBlock Text="Author"
VerticalAlignment="Bottom"
RelativePanel.AlignRightWithPanel="True"
FontSize="12"
HorizontalAlignment="Stretch"
Margin="0,0,8,0"></TextBlock>
</RelativePanel>
<TextBlock x:Name="nameTextBlock"
Grid.Column="1" Grid.Row="1"
Text="Name"
TextTrimming="WordEllipsis"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
FontSize="18" >
</TextBlock>
<Grid x:Name="bottomGrid"
Grid.Column="1" Grid.Row="2"
VerticalAlignment="Top"
HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="3*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
HorizontalAlignment="Stretch"
Text="90"></TextBlock>
<TextBlock Grid.Column="1"
HorizontalAlignment="Stretch"
Text="48"></TextBlock>
<TextBlock Grid.Column="2"
HorizontalAlignment="Stretch"
Text="20170330 08:33"
Margin="0,0,0,0"></TextBlock>
</Grid>
<Border Background="LightGray"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2"></Border>
我在 6inch 模拟器上测试了这些代码,我添加了这个例子的截图。
并且移动模拟器没有相同的结果。
这是不同的模拟器屏幕截图。
4inch
screen shot of example
这些屏幕截图的右侧不同。
我认为,这是因为 ListView.ItemTemplate 控件具有 MaxWidth="400" 属性。
I try to stretch listview item in horizontal orientation.
您需要为 ListViewItem
设置 HorizontalContentAlignment
属性,而不是 HorizontalAlignment
。使用以下代码 ListViewItem
的内容将被拉伸。
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListView.ItemContainerStyle>
默认ListViewItem
style and template不打开HorizontalAlignment
setter但是HorizontalContentAlignment
.
开发人员。
我尝试在水平方向上拉伸列表视图项目。 我确实重置了 ItemContainerStyle ,并且水平对齐是 strech。 这是 MainPage.xaml.
中的所有代码<Grid x:Name="rootGrid"
HorizontalAlignment="Stretch"
MinWidth="320"
MaxWidth="480"
Width="Auto">
<ListView x:Name="infoFlowListView"
MinWidth="320"
MaxWidth="480"
Width="Auto"
HorizontalAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<local:MyUserControl1 MinWidth="300" HorizontalAlignment="Stretch"></local:MyUserControl1>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Grid>
这里是MainPage.xaml.cs中的所有代码
public MainPage()
{
var items = new List<MyUserControl1>();
for (int i = 0; i < 50; i++)
{
items.Add(new MyUserControl1());
}
this.InitializeComponent();
infoFlowListView.ItemsSource = items;
}
这是MyUserControl.xaml
中的所有代码<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="4*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="8"></RowDefinition>
</Grid.RowDefinitions>
<Ellipse Width="60" Height="60"
Grid.Column="0" Grid.Row="0" Grid.RowSpan="3"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Stretch="Uniform">
<Ellipse.Fill>
<ImageBrush ImageSource="Assets/1.png"></ImageBrush>
</Ellipse.Fill>
</Ellipse>
<RelativePanel x:Name="topRelativePanel"
Grid.Column="1" Grid.Row="0"
HorizontalAlignment="Stretch">
<TextBlock Text="TestName"
VerticalAlignment="Bottom"
RelativePanel.AlignLeftWithPanel="True"
FontSize="12"
HorizontalAlignment="Stretch"></TextBlock>
<TextBlock Text="Author"
VerticalAlignment="Bottom"
RelativePanel.AlignRightWithPanel="True"
FontSize="12"
HorizontalAlignment="Stretch"
Margin="0,0,8,0"></TextBlock>
</RelativePanel>
<TextBlock x:Name="nameTextBlock"
Grid.Column="1" Grid.Row="1"
Text="Name"
TextTrimming="WordEllipsis"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
FontSize="18" >
</TextBlock>
<Grid x:Name="bottomGrid"
Grid.Column="1" Grid.Row="2"
VerticalAlignment="Top"
HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="3*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
HorizontalAlignment="Stretch"
Text="90"></TextBlock>
<TextBlock Grid.Column="1"
HorizontalAlignment="Stretch"
Text="48"></TextBlock>
<TextBlock Grid.Column="2"
HorizontalAlignment="Stretch"
Text="20170330 08:33"
Margin="0,0,0,0"></TextBlock>
</Grid>
<Border Background="LightGray"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2"></Border>
我在 6inch 模拟器上测试了这些代码,我添加了这个例子的截图。
并且移动模拟器没有相同的结果。 这是不同的模拟器屏幕截图。 4inch screen shot of example 这些屏幕截图的右侧不同。
我认为,这是因为 ListView.ItemTemplate 控件具有 MaxWidth="400" 属性。
I try to stretch listview item in horizontal orientation.
您需要为 ListViewItem
设置 HorizontalContentAlignment
属性,而不是 HorizontalAlignment
。使用以下代码 ListViewItem
的内容将被拉伸。
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListView.ItemContainerStyle>
默认ListViewItem
style and template不打开HorizontalAlignment
setter但是HorizontalContentAlignment
.