簡體   English   中英

ListBox ItemsSource在XAML中不起作用

[英]ListBox ItemsSource doesn't work from XAML

我正在嘗試從xaml綁定列表框,但似乎我在做錯什么。 這是我的XAML頁面。

<Grid x:Name="LayoutRoot" Background="Transparent">
<!--Pivot Control-->
<phone:Pivot Title="MY APPLICATION">
<!--Pivot item two-->
<phone:PivotItem Header="item2">
<Grid>
  <ListBox Foreground="Black" x:Name="ls" ItemsSource="{Binding MyLang}" >
    <ListBox.ItemTemplate>
      <DataTemplate>
        <TextBlock Text="{Binding Language}"></TextBlock>
      </DataTemplate>
   </ListBox.ItemTemplate>
  </ListBox>
</Grid>
</phone:PivotItem>
</phone:Pivot>
</Grid>

背后的代碼

DataSource ds = null;
public List<TranslationLanguage> MyLang;
public PivotPage2()
{
    ds = new DataSource();
    MyLang = ds.getTranslationLanguages(); //return collections and contains 13 rows
    InitializeComponent();
    DataContext = this;
}
public class TranslationLanguage
{
        [PrimaryKey]
        public byte LanguageID{get;set;}
        public string Language{get;set;}
        public string Description { get; set; }
        public bool IsActive { get; set; }
        public FlowDirection FlowDirection { get; set; }
        public string DownloadURL { get; set; }
        public bool IsDownloaded { get; set; }
        public string ImagePath { get; set; }
}

我真的很困惑,為什么它根本不起作用?

綁定只能在屬性上完成, MyLang不是屬性。 MyLang設為財產

public List<TranslationLanguage> MyLang{ get; set;};

由於您的數據上下文與后面的代碼綁定在一起,因此需要將Window的Datacontext設置為

DataContext="{Binding RelativeSource={RelativeSource Self}}

您必須這樣做:

DataContext="{Binding RelativeSource={RelativeSource Self}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM