如何将 XAML 中的绑定上下文设置为不同的 class?
How can I set up binding context in XAML to a different class?
我有这段代码,我正在尝试绑定到名为 BandInfoRepository.cs 的 class,它位于与名为 PaginaB.I 的 XAML 相同的文件夹中看不到 VisualStudio 上没有显示任何语法错误,仍然没有显示文本(我添加 backgroundColor 只是为了查看标签是否正在显示并且它们是,但文本不是)。
也许指出我正在使用 syncfusion 的列表视图很重要。
PaginaB.xaml :
<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>
这是 BandInfoRepository.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" });
}
}
在您的 DataTemplate 中,您通常不会在绑定中设置 Source,除非您想施展魔法。 XAML 将 DataContext 设置为 ItemsSource 的每个项目。
尝试:
<Label Text="{Binding BandName}" BackgroundColor="Olive" FontAttributes="Bold" />
如果您希望 XAML 跟踪其属性的变化,请记住为 BandInfo 实施 INotifyPropertyChanged
感谢您使用 Syncfusion 产品。
我们查看了您的代码,发现您错误地定义了 ItemTemplate。您可以将底层集合中的数据对象直接绑定到 ItemTemplate 属性 中定义的视图中。 SfListView 本身为 ItemsSource 属性 中的每个项目创建一个视图,并为其定义绑定上下文。
为了您的参考,我们附上了示例,您可以从下面的链接下载它link。
样本:http://www.syncfusion.com/downloads/support/directtrac/general/ze/ListViewSample607957192
更多关于使用SfListView的信息,请参考以下UG文档link。
https://help.syncfusion.com/xamarin/sflistview/getting-started
如果您需要进一步的帮助,请告诉我们。
此致,
迪内什·巴布·亚达夫
我有这段代码,我正在尝试绑定到名为 BandInfoRepository.cs 的 class,它位于与名为 PaginaB.I 的 XAML 相同的文件夹中看不到 VisualStudio 上没有显示任何语法错误,仍然没有显示文本(我添加 backgroundColor 只是为了查看标签是否正在显示并且它们是,但文本不是)。
也许指出我正在使用 syncfusion 的列表视图很重要。
PaginaB.xaml :
<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>
这是 BandInfoRepository.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" });
}
}
在您的 DataTemplate 中,您通常不会在绑定中设置 Source,除非您想施展魔法。 XAML 将 DataContext 设置为 ItemsSource 的每个项目。
尝试:
<Label Text="{Binding BandName}" BackgroundColor="Olive" FontAttributes="Bold" />
如果您希望 XAML 跟踪其属性的变化,请记住为 BandInfo 实施 INotifyPropertyChanged
感谢您使用 Syncfusion 产品。
我们查看了您的代码,发现您错误地定义了 ItemTemplate。您可以将底层集合中的数据对象直接绑定到 ItemTemplate 属性 中定义的视图中。 SfListView 本身为 ItemsSource 属性 中的每个项目创建一个视图,并为其定义绑定上下文。
为了您的参考,我们附上了示例,您可以从下面的链接下载它link。
样本:http://www.syncfusion.com/downloads/support/directtrac/general/ze/ListViewSample607957192
更多关于使用SfListView的信息,请参考以下UG文档link。 https://help.syncfusion.com/xamarin/sflistview/getting-started
如果您需要进一步的帮助,请告诉我们。
此致, 迪内什·巴布·亚达夫