解决创建自定义网格视图模板
public class AdaptableGridView : GridView
{
// default itemWidth
private const double itemWidth = 100.00;
public double ItemWidth
{
get { return (double)GetValue(ItemWidthProperty); }
set { SetValue(ItemWidthProperty, value); }
}
public static readonly DependencyProperty ItemWidthProperty =
DependencyProperty.Register("ItemWidth", typeof(double), typeof(AdaptableGridView), new PropertyMetadata(itemWidth));
// default max number of rows or columns
private const int maxRowsOrColumns = 3;
public int MaxRowsOrColumns
{
get { return (int)GetValue(MaxRowColProperty); }
set { SetValue(MaxRowColProperty, value); }
}
public static readonly DependencyProperty MaxRowColProperty =
DependencyProperty.Register("MaxRowsOrColumns", typeof(int), typeof(AdaptableGridView), new PropertyMetadata(maxRowsOrColumns));
public AdaptableGridView()
{
this.SizeChanged += MyGridViewSizeChanged;
}
private void MyGridViewSizeChanged(object sender, SizeChangedEventArgs e)
{
// Calculate the proper max rows or columns based on new size
this.MaxRowsOrColumns = this.ItemWidth > 0 ? Convert.ToInt32(Math.Floor(e.NewSize.Width/this.ItemWidth)) : maxRowsOrColumns;
}
}
XAML:
<local:AdaptableGridView ItemWidth="250" x:Name="tumbview" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ItemsSource="{Binding imagelist}" SelectionChanged="GridView_SelectionChanged" Margin="50,0,0,0" >
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid Orientation="Horizontal"
ItemWidth="{Binding ElementName=tumbview, Path=ItemWidth}"
MaximumRowsOrColumns="{Binding ElementName=tumbview, Path=MaxRowsOrColumns}"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
很好的教程在:custom grid view tutorial
你在此刻得到了什么?如果你的gridview水平滚动,那意味着内容比屏幕宽。要使它垂直滚动,您可能必须重新排列内容以使其更窄。没有看到你当前的XAML,很难提出任何建议。 – ChrisF
我发布了xaml –