ListBox ItemsSource不适用于XAML

时间:2014-03-28 11:11:43

标签: c# windows-phone-8

我正在尝试从xaml绑定listbox,但似乎我做错了。这是我的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; }
}

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

3 个答案:

答案 0 :(得分:4)

绑定只能在属性上进行,MyLang不是属性。将MyLang设为属性

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

答案 1 :(得分:0)

由于你从后面的代码绑定了datacontext,你需要将Window的Datacontext设置为

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

答案 2 :(得分:-1)

你必须这样做:

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