【问题标题】:How can I set up binding context in XAML to a different class?如何在 XAML 中将绑定上下文设置为不同的类?
【发布时间】:2017-08-30 18:42:51
【问题描述】:

我有这段代码,我正在尝试绑定到名为 BandInfoRepository.cs 的类,该类与名为 PaginaB 的 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" });
    }
}

【问题讨论】:

    标签: xaml xamarin data-binding syncfusion


    【解决方案1】:

    在您的 DataTemplate 中,您通常不会在绑定中设置 Source,除非您想做一些魔术。 XAML 将 DataContext 设置为 ItemsSource 的每个项目。

    试试:

     <Label Text="{Binding BandName}" BackgroundColor="Olive" FontAttributes="Bold" />
    

    如果您希望 XAML 跟踪其属性的更改,请记住为 BandInfo 实现 INotifyPropertyChanged

    【讨论】:

      【解决方案2】:

      感谢您使用 Syncfusion 产品。

      我们查看了您的代码,发现您错误地定义了 ItemTemplate。您可以将基础集合中的数据对象直接绑定到 ItemTemplate 属性中定义的视图中。 SfListView 本身为 ItemsSource 属性中的每个项目创建一个视图,并为其定义绑定上下文。

      我们已附上示例供您参考,您可以从以下链接下载。

      示例:http://www.syncfusion.com/downloads/support/directtrac/general/ze/ListViewSample607957192

      有关使用 SfListView 的更多信息,请参阅以下 UG 文档链接。 https://help.syncfusion.com/xamarin/sflistview/getting-started

      如果您需要进一步的帮助,请告诉我们。

      问候, 迪内什·巴布·亚达夫

      【讨论】:

      • user3512524 已经帮助我解决了这个问题,并且解决了,我是数据绑定的新手。不过,感谢您的回答,我会下载您链接的示例,看看是否可以从那里改进我的代码。
      猜你喜欢
      • 2019-06-10
      • 2017-03-15
      • 1970-01-01
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      • 2014-08-16
      相关资源
      最近更新 更多