[英]How to create DataTemplate in UWP platform using C# code behind?
I need to create the DataTemplate in UWP platform using C# code behind. 我需要使用后面的C#代码在UWP平台中创建DataTemplate。 I have tried most of the solution given in the WPF platform so please anyone can share you idea to create dataTemplate in UWP platform using c# as code behind.
我已经尝试了WPF平台中提供的大多数解决方案,所以请任何人都可以分享您的想法,并使用c#作为背后的代码在UWP平台中创建dataTemplate。
If you really want to create a DataTemplate
from code-behind, please see this answer . 如果您真的想从后台代码创建
DataTemplate
,请参阅此答案 。 But unless you have a specific reason to do that, it's much simpler to create it in XAML, for example in your Page.Resources
. 但是,除非有特定的原因,否则在XAML中(例如在
Page.Resources
创建它要简单得多。
<Page (all the xmlns declarations here) >
<Page.Resources>
<DataTemplate x:Key="templateEmployee">
<StackPanel>
<TextBlock Text="My Name"/>
<TextBlock Text="My Age"/>
</StackPanel>
</DataTemplate>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView Name="thing">
...
</ListView>
</Grid>
</Page>
And from your code-behind : 并且从您的代码背后:
public MainPage()
{
this.InitializeComponent();
thing.ItemTemplate = (DataTemplate)this.Resources["templateEmployee"];
thing.Items.Add(new object());
thing.Items.Add(new object());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.