[英]Dynamically creating GridViewItem XAML
I have a list that will not always have the same number of objects in it, could be 1, 10, 15, etc. Right now I am just guessing and placing that guessed amount of GridviewItems
in my XAML
. 我有一个列表,该列表中的对象数量不一定总是相同的,可能是
GridviewItems
等。现在,我只是在猜测并在XAML
放置该猜测的GridviewItems
数量。 I want to dynamically build the GridviewItems
based on the amount of objects in my list. 我想根据列表中对象的数量动态构建
GridviewItems
。 I have have this in a loop of course, I am just unsure of how to do this in c#
. 我当然有这个循环,只是不确定如何在
c#
执行此操作。 I will show you the XAML I have right now that is not dynamically creating the GridviewItems
. 我将向您展示当前没有动态创建
GridviewItems
的XAML。
<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 Service 网络服务
[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;
}
Project where I am calling web service 我正在呼叫网络服务的项目
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;
}
}
What I am looking for is some way to programatically build the GridViewItems
... Any suggestions? 我正在寻找某种以编程方式构建
GridViewItems
...有什么建议吗?
Just bind ItemsSource
只需绑定
ItemsSource
<GridView Margin="12,60" ItemsSource={Binding ItemsList}>
...
</GridView>
EDIT: 编辑:
1) You can use DataGrid like this 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) Or ListView 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) Or ListBox 3)或列表框
<ListBox ItemsSource="{Binding TestDataCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding AuthorView}"/>
<TextBlock Text="{Binding TitleView}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.