gpt4 book ai didi

c# - 通过x :bind in UWP绑定(bind)数据到ListView

转载 作者:太空狗 更新时间:2023-10-30 00:49:23 25 4
gpt4 key购买 nike

我在我的程序和 <HubSection> 中使用 hubsection有一个 ListView 。但我无法将数据绑定(bind)到 ListView。我曾尝试使用 {binding}但是我在输出和使用 x:bind 时变得空白我收到的错误是

No DataType defined for DataTemplate. Templates containing x:Bind need a DataType to be specified using 'x:DataType'

帮我解决这个问题。这是我的代码:

.xaml

    <Hub Header="abc" FontWeight="Bold">
<HubSection Header="header1" x:Name="hub1">
<DataTemplate >
<ListView x:Name="list1" ItemsSource="{x:Bind info}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Image Source="{X:Bind image}"></Image>
<TextBlock Text="{x:Bind name}"/>
</StackPanel>
<TextBlock Text="{x:Bind bio}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</HubSection>
<HubSection Header="header 2">
<DataTemplate>
</DataTemplate>
</HubSection>
</Hub>
</Grid>

.cs

namespace app1
{
public class info
{
public String name { get; set; }
public String bio { get; set; }
public String image { get; set; }
}

public sealed partial class abcde : Page
{

ObservableCollection<info> obsinfo = new ObservableCollection<info>();
public abcde()
{
this.InitializeComponent();
filldata();
}
void filldata()
{
obsinfo.Add(new info { name = "data1", image = "images/data1.png", bio = "" });
obsinfo.Add(new info { name = "data2", image = "images/data2.png", bio = "" });

}
}
}

最佳答案

基本上,您得到的错误是告诉您您没有定义要绑定(bind)到它的数据类型。

因此,为了在 DataTemplate 中解决此问题,请将此属性添加到您的:ListView.ItemTemplate -> DataTemplate

x:DataType="namespace:DATA_TYPE"

对于此示例,您的类 info 与 MainPage 位于同一命名空间中,因此对于 XAML 中的命名空间,我将为命名空间设置 local,如下所示:

<DataTemplate x:DataType="local:info"

而且你在这部分也犯了错误:

ItemsSource="{x:Bind info}"

在这里你需要设置你想要绑定(bind)的列表或对象而不是数据类型,类信息显然是你的数据类型。

另一件事是,您不能只告诉 HubControl 中的 ItemsSource,您需要使用某种加载事件以编程方式设置它,并且在加载事件中您可以设置 ItemSource。

因此,在您进行所有修复的情况下,您的 XAML 应该如下所示,这是针对 XAML 和 .CS 的经过测试和工作的代码:

<Hub Header="abc" FontWeight="Bold">
<HubSection Header="header1" x:Name="hub1">
<DataTemplate>
<!-- Instead of ItemSource in XAML we make event in which we will set ItemSource -->
<ListView x:Name="list1"
Loaded="Data_Loaded">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:info">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Image Source="{X:Bind image}"></Image>
<TextBlock Text="{x:Bind name}"/>
</StackPanel>
<TextBlock Text="{x:Bind bio}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</HubSection>
<HubSection Header="header 2">
<DataTemplate>
</DataTemplate>
</HubSection>
</Hub>
</Grid>

您的 .cs 分部类:

namespace app1
{
public class info
{
public String name { get; set; }
public String bio { get; set; }
public String image { get; set; }
}

public sealed partial class abcde : Page
{

ObservableCollection<info> obsinfo = new ObservableCollection<info>();
public abcde()
{
this.InitializeComponent();
filldata();
}
void filldata()
{
obsinfo.Add(new info { name = "data1", image = "images/data1.png", bio = "" });
obsinfo.Add(new info { name = "data2", image = "images/data2.png", bio = "" });

}

// Here we can set ItemsSource for our ListView
private void Data_Loaded(object sender, RoutedEventArgs e) {
var listView = (ListView)sender;
listView.ItemsSource = obsinfo;
}

}
}

进行这些更改并运行它,更改后它应该可以工作。

注意:在 x:DataType 属性中小心设置您的类 info 所在的命名空间,以便正常工作。

更改后,如果您在 XAML 中看到蓝线,请清理并重建您的项目,我强烈建议您进行代码分离。

另外我给你的提示是使用 Pivot 控件来实现这种“显示数据”,它更容易实现并且你会得到类似的结果。你可以看看here .

关于c# - 通过x :bind in UWP绑定(bind)数据到ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38520592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com