绑定不起作用

时间:2014-05-19 03:57:56

标签: c# windows xaml

我试图以表格的形式显示名称和滚动号的记录,但我只是空白而不是存储在对象中的实际字符串。

 public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    private void runthis(object sender, RoutedEventArgs e)
    {
        name per= new name();
        per.nam = "sujit";
        per.roll = 1;
        name cl = new name();
        cl.nam = "sumit";
        cl.roll = 2;
        observable.lst.Add(per);
        observable.lst.Add(cl);
        lstview.DataContext = observable.lst; 

    }

    private void add(object sender, RoutedEventArgs e)
    {
        name cl = new name();
        cl.nam = "sumit";
        observable.lst.Add(cl);
    }

   }
public class name
{
    public string nam;
    public int roll;



}
public static class observable
{
    public static ObservableCollection<name> lst = new ObservableCollection<name>();
}

xaml代码如下

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Loaded="runthis">
    <ListView x:Name="lstview" 
  VerticalAlignment="Top"
  Margin="10,240,0,0"
  ItemsSource="{Binding}"

  BorderBrush="#19FFFFFF" BorderThickness="1,0,0,1"  >
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding nam}"/>
                    <TextBlock Text="{Binding roll}"/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>

    </ListView>
    <Button Content="Add" Click="add"/>
</Grid>

任何人都可以在此代码中指出错误,导致只显示空白。

1 个答案:

答案 0 :(得分:0)

XAML不支持DataBinding到字段。只有属性。像这样更改你的代码:

public class Name
{
    public string Nam {get;set;}
    public int Roll {get;set;}
}
顺便说一句,请使用正确的C#命名约定,类和类成员应该是ProperCased。