我有一个列表,它不会总是包含相同数量的对象,可能是1个,10个,15个等等。现在我只是猜测并将猜测量的GridviewItems
放在我的{ {1}}。我想根据列表中的对象数量动态构建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>
我呼叫网络服务的项目
[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;
}
我正在寻找的是以编程方式构建 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;
}
}
的某种方式...任何建议?
答案 0 :(得分:2)
绑定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>