代码之家  ›  专栏  ›  技术社区  ›  Mauricio Cárdenas

如何在XAML中设置绑定上下文到其他类?

  •  0
  • Mauricio Cárdenas  · 技术社区  · 7 年前

    我有一段代码,我正试图绑定到一个名为BandInfoRepository的类。cs,它与名为PaginaB的XAML位于同一文件夹中。我看不到VisualStudio上没有显示任何语法错误,但仍然没有显示文本(我添加backgroundColor只是为了查看标签是否正在显示,但文本没有显示)。

    也许有必要指出我使用的是syncfusion的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>
    

    这是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" });
        }
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Krzysztof Skowronek    7 年前

    在DataTemplate中,通常不会在绑定中设置源代码,除非您想施展魔法。XAML为ItemsSource的每个项设置DataContext。

    尝试:

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

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

        2
  •  0
  •   Dinesh    7 年前

    为供您参考,我们附上了样品,您可以从下面的链接下载。

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

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

    Dinesh Babu Yadav