如何在XAML中将绑定上下文设置为其他类?
我有一段代码,我正试图绑定到一个名为BandInformationPository.cs的类,该类与名为PaginaB的XAML位于同一文件夹中。我看不到VisualStudio上显示的语法错误,但文本仍然没有显示(我添加backgroundColor只是为了查看标签是否显示,但文本没有显示) 也许有必要指出我使用的是syncfusion的listview PaginaB.xaml:如何在XAML中将绑定上下文设置为其他类?,xaml,xamarin,data-binding,syncfusion,Xaml,Xamarin,Data Binding,Syncfusion,我有一段代码,我正试图绑定到一个名为BandInformationPository.cs的类,该类与名为PaginaB的XAML位于同一文件夹中。我看不到VisualStudio上显示的语法错误,但文本仍然没有显示(我添加backgroundColor只是为了查看标签是否显示,但文本没有显示) 也许有必要指出我使用的是syncfusion的listview PaginaB.xaml: <syncfusion:SfListView x:Name="listView"
<syncfusion:SfListView x:Name="listView"
ItemsSource="{Binding Source={local2:BandInfoRepository}, Path=BandInfo}"
ItemSize="100"
AbsoluteLayout.LayoutBounds="1,1,1,1"
AbsoluteLayout.LayoutFlags="All" >
<syncfusion:SfListView.ItemTemplate>
<DataTemplate>
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="0.4*" />
<RowDefinition Height="0.6*" />
</Grid.RowDefinitions>
<Label Text="{Binding Source={local2:BandInfoRepository}, Path=BandName}"
BackgroundColor="Olive"
FontAttributes="Bold"
TextColor="Black"
FontSize="20" />
<Label Grid.Row="1"
BackgroundColor="Navy"
Text="{Binding Source={local2:BandInfoRepository}, Path= BandDescription}"
TextColor="Black"
FontSize="14"/>
</Grid>
</DataTemplate>
</syncfusion:SfListView.ItemTemplate>
</syncfusion:SfListView>
这是bandinformepository.cs文件:
public class BandInfoRepository
{
private ObservableCollection<BandInfo> bandInfo;
public ObservableCollection<BandInfo> BandInfo
{
get { return bandInfo; }
set { this.bandInfo = value; }
}
public BandInfoRepository()
{
GenerateBookInfo();
}
internal void GenerateBookInfo()
{
bandInfo = new ObservableCollection<BandInfo>();
bandInfo.Add(new BandInfo() { BandName = "Nirvana", BandDescription = "description" });
bandInfo.Add(new BandInfo() { BandName = "Metallica", BandDescription = "description" });
bandInfo.Add(new BandInfo() { BandName = "Frank Sinatra", BandDescription = "description" });
bandInfo.Add(new BandInfo() { BandName = "B.B. King", BandDescription = "description" });
bandInfo.Add(new BandInfo() { BandName = "Iron Maiden", BandDescription = "description" });
bandInfo.Add(new BandInfo() { BandName = "Megadeth", BandDescription = "description" });
bandInfo.Add(new BandInfo() { BandName = "Darude", BandDescription = "description" });
bandInfo.Add(new BandInfo() { BandName = "Coldplay", BandDescription = "description" });
bandInfo.Add(new BandInfo() { BandName = "Dream Evil", BandDescription = "description" });
bandInfo.Add(new BandInfo() { BandName = "Pentakill", BandDescription = "description" });
}
}
公共类BandInformationLocatory
{
私有可观察收集bandInfo;
公共可观测收集波段信息
{
获取{return bandInfo;}
设置{this.bandInfo=value;}
}
公共信息库()
{
GenerateBookInfo();
}
内部void GenerateBookInfo()
{
bandInfo=新的ObservableCollection();
添加(新的bandInfo(){BandName=“Nirvana”,BandDescription=“description”});
添加(新的bandInfo(){BandName=“Metallica”,BandDescription=“description”});
添加(新的bandInfo(){BandName=“Frank Sinatra”,BandDescription=“description”});
添加(新的bandInfo(){BandName=“B.B.King”,BandDescription=“description”});
添加(新的bandInfo(){BandName=“Iron Maiden”,BandDescription=“description”});
添加(新的bandInfo(){BandName=“Megadeth”,BandDescription=“description”});
添加(新的bandInfo(){BandName=“Darude”,BandDescription=“description”});
添加(新的bandInfo(){BandName=“Coldplay”,BandDescription=“description”});
添加(新的bandInfo(){BandName=“Dream Evil”,BandDescription=“description”});
添加(新的bandInfo(){BandName=“Pentakill”,BandDescription=“description”});
}
}
在DataTemplate中,除非您想施展魔法,否则通常不会在绑定中设置源代码。XAML为ItemsSource的每个项设置DataContext
尝试:
如果希望XAML跟踪其属性的更改,请记住为BandInfo实现INotifyPropertyChanged感谢您使用Syncfusion产品 我们查看了您的代码,发现您错误地定义了ItemTemplate。可以将基础集合中的数据对象直接绑定到ItemTemplate属性中定义的视图中。SfListView本身为ItemsSource属性中的每个项创建一个视图,并定义其绑定上下文 为了您的参考,我们附上了样本,您可以从下面的链接下载 样本: 有关使用SfListView的更多信息,请参考以下UG文档链接。 如果您需要进一步帮助,请告知我们 问候,,
Dinesh Babu Yadavuser3512524已经帮助我解决了这个问题,我对数据绑定还不熟悉。不过,谢谢你的回答,我会下载你链接的示例,看看能否从那里改进我的代码。
<Label Text="{Binding BandName}" BackgroundColor="Olive" FontAttributes="Bold" />