动态创建GridViewItem XAML
本文关键字:XAML GridViewItem 创建 动态 | 更新日期: 2023-09-27 18:24:08
我有一个列表,其中的对象数量并不总是相同,可能是1、10、15等。现在我只是猜测并将猜测的GridviewItems
数量放在我的XAML
中。我想根据列表中的对象数量动态构建GridviewItems
。当然,我已经有了一个循环,我只是不确定如何在c#
中做到这一点。我将向您展示我现在拥有的XAML,它不是动态创建GridviewItems
的。
<Grid Background="LightGray">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<controls:PageHeader BackButtonVisibility="Collapsed" Content="News" Frame="{x:Bind Frame}">
<Interactivity:Interaction.Behaviors>
<Behaviors:EllipsisBehavior Visibility="Auto" />
</Interactivity:Interaction.Behaviors>
<controls:PageHeader.SecondaryCommands>
<AppBarButton Click="{x:Bind ViewModel.GotoPrivacy}" Label="Privacy" />
<AppBarButton Click="{x:Bind ViewModel.GotoAbout}" Label="About" />
</controls:PageHeader.SecondaryCommands>
</controls:PageHeader>
<GridView Margin="12,60" ItemsSource="{Binding myList}">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid Orientation="Vertical" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Background="#2A2A2A" Margin="5" Height="200" Width="300">
<ContentPresenter />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
</GridView.ItemContainerStyle>
</GridView>
</Grid>
Web服务
[OperationContract]
List<ViewDetails> ViewDetails();
[DataContract]
public class ViewDetails
{
public string TitleView { get; set; }
public string BodyView { get; set; }
public string AuthorView { get; set; }
public ViewDetails() { }
public ViewDetails(string myTitleView, string myBodyView, string myAuthorView)
{
this.TitleView = myTitleView;
this.BodyView = myBodyView;
this.AuthorView = myAuthorView;
}
}
public List<ViewDetails> ViewDetails()
{
List<ViewDetails> details = new List<ViewDetails>();
SqlConnection conn = new SqlConnection(strConnString);
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT TOP 2 [My_Title] AS 'Title', [My_Body] AS 'Body', [My_Author] AS 'Author' FROM [My_table] ORDER BY [Date] DESC", conn);
SqlDataReader rdrDetails = cmd.ExecuteReader();
try
{
while (rdrDetails.Read())
{
details.Add(new ViewDetails(rdrDetails.GetSqlString(rdrDetails.GetOrdinal("Title")).ToString(), rdrDetails.GetSqlString(rdrDetails.GetOrdinal("Body")).ToString(), rdrDetails.GetSqlString(rdrDetails.GetOrdinal("Author")).ToString()));
}
}
catch (Exception e)
{
//exception
}
finally
{
conn.Close();
}
return details;
}
我调用web服务的项目
public async void ViewData()
{
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
string title = string.Empty;
string body = string.Empty;
string author = string.Empty;
var res = await client.ViewDetailsAsync();
for (int i = 0; i < res.Count; i++)
{
myList.Add(new GetDetails(res[i].TitleView, res[i].BodyView, res[i].AuthorView));
}
}
public class GetDetails
{
public string TitleView { get; set; }
public string BodyView { get; set; }
public string AuthorView { get; set; }
public GetDetails() { }
public GetDetails(string titleView, string bodyView, string authorView)
{
this.TitleView = titleView;
this.BodyView = bodyView;
this.AuthorView = authorView;
}
}
我正在寻找的是用程序化的方式构建GridViewItems
。。。有什么建议吗?
只需绑定ItemsSource
<GridView Margin="12,60" ItemsSource={Binding ItemsList}>
...
</GridView>
编辑:
1) 你可以像这个一样使用DataGrid
<DataGrid ItemsSource="{Binding TestDataCollection}">
<DataGrid.Columns>
<DataGridTextColumn Header="Title" Binding="{Binding TitleView}"/>
<DataGridTextColumn Header="Body" Binding="{Binding BodyView}"/>
<DataGridTextColumn Header="Author" Binding="{Binding AuthorView}"/>
</DataGrid.Columns>
</DataGrid>
2) 或ListView
<ListView ItemsSource="{Binding TestDataCollection}">
<ListView.View>
<GridView>
<GridViewColumn Header="Author" DisplayMemberBinding="{Binding Path=AuthorView}"/>
<GridViewColumn Header="Title" DisplayMemberBinding="{Binding Path=TitleView}"/>
</GridView>
</ListView.View>
</ListView>
3) 或ListBox
<ListBox ItemsSource="{Binding TestDataCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding AuthorView}"/>
<TextBlock Text="{Binding TitleView}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>