首页 新闻 搜索 专区 学院

WP7 listbox 绑定后不显示数据

0
[待解决问题]

MainPage.XAML.cs:

Model m = new Model();
            m = new Model("静夜思", "李白", "床前明月光", "疑是地上霜", "举头望明月", "低头思故乡");
            this.LayoutRoot.DataContext = m;

MainPage.xaml:

    <phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Key="ListBoxControlTemplate1">
            <Grid Margin="0,0,0,-5">
                <TextBlock TextWrapping="Wrap" Text="{Binding Title}" VerticalAlignment="Top" d:LayoutOverrides="Width" Padding="100,10,0,10" Height="60" FontFamily="Tahoma" FontSize="37.333"/>
                <TextBlock TextWrapping="Wrap" Text="{Binding Author}" VerticalAlignment="Top" Margin="0,64,0,0" d:LayoutOverrides="Width" Padding="200,10,0,10" Height="50" FontFamily="Tahoma" FontSize="24"/>
                <TextBlock TextWrapping="Wrap" Text="{Binding Txt1}" Margin="0,0,0,128" d:LayoutOverrides="Width" Padding="70,10,0,10" Height="50" VerticalAlignment="Bottom" FontSize="29.333" FontFamily="Tahoma"/>
                <TextBlock TextWrapping="Wrap" Text="{Binding Txt2}" VerticalAlignment="Bottom" Margin="0,0,0,178" d:LayoutOverrides="Width" Padding="70,10,0,10" Height="50" FontFamily="Tahoma" FontSize="29.333"/>
                <TextBlock TextWrapping="Wrap" Text="{Binding Txt3}" Margin="0,184,0,228" d:LayoutOverrides="Width" Padding="70,10,0,10" FontSize="29.333" FontFamily="Tahoma"/>
                <TextBlock TextWrapping="Wrap" Text="{Binding Txt4}" Margin="0,130,0,0" d:LayoutOverrides="Width" Padding="70,10,0,10" Height="50" VerticalAlignment="Top" FontFamily="Tahoma" FontSize="29.333"/>
            </Grid>
        </DataTemplate>
    </phone:PhoneApplicationPage.Resources>

    <!--LayoutRoot 是包含所有页面内容的根网格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel 包含应用程序的名称和页标题-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="唐诗三百首" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - 在此处放置其他内容-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button Content="上一首" HorizontalAlignment="Left" Margin="46,0,0,36" VerticalAlignment="Bottom" x:Name="btnPre"/>
            <Button Content="下一首" HorizontalAlignment="Right" Margin="0,0,48,36" VerticalAlignment="Bottom" x:Name="btnNext"/>
            <ListBox Margin="46,19,48,126" x:Name="listbox1" ItemTemplate="{StaticResource ListBoxControlTemplate1}"  ItemsSource="{Binding Mode=OneWay}">
            </ListBox>
        </Grid>
    </Grid>

Model.cs:

partial class Model : INotifyPropertyChanged
    {
        public Model() { }
        private string title;

        public string Title
        {
            get { return title; }
            set
            {
                title = value;
                INotifyPropertyChanged("Title");
            }
        }

        private string author;

        public string Author
        {
            get { return author; }
            set
            {
                author = value;
                INotifyPropertyChanged("Author");
            }
        }

        private string txt1;

        public string Txt1
        {
            get { return txt1; }
            set { txt1 = value; INotifyPropertyChanged("Txt1"); }
        }

        private string txt2;

        public string Txt2
        {
            get { return txt2; }
            set { txt2 = value; INotifyPropertyChanged("Txt2"); }
        }

        private string txt3;

        public string Txt3
        {
            get { return txt3; }
            set { txt3 = value; INotifyPropertyChanged("Txt3"); }
        }

        private string txt4;

        public string Txt4
        {
            get { return txt4; }
            set { txt4 = value; INotifyPropertyChanged("Txt4"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void INotifyPropertyChanged(string p)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(p));
            }
        }

        public Model(string title, string author, string txt1, string txt2, string txt3, string txt4)
        {
            this.Title = title;
            this.Author = author;
            this.Txt1 = txt1;
            this.Txt2 = txt2;
            this.Txt3 = txt3;
            this.Txt4 = txt4;
        }
    }

为什么绑定后就是不显示数据?

乄蛇的主页 乄蛇 | 初学一级 | 园豆:22
提问于:2012-04-17 22:47
< >
分享
所有回答(3)
0

我的可以绑定的啊。可能是你的资源设置错误了啊。
我猜是这样的。即使你绑定了集合,但是你的前台代码的DataContent可能设置忘了吧。

不好意思,刚重新看了一下,你的数据绑定了,我没看出来啊。请多多指教啊。

杨恒连2011 | 园豆:186 (初学一级) | 2012-04-17 23:51
0

把你的MainPage.XAML.cs:改一下,如下:

Model m = new Model();
m = new Model("静夜思", "李白", "床前明月光", "疑是地上霜", "举头望明月", "低头思故乡");
List<Model> lm = new List<Model>();
lm.Add(m);
this.listbox1.ItemsSource = lm;

然后把MainPage.xaml:里的

ItemsSource="{Binding Mode=OneWay}"

删掉。

因为ListBox 的Itemsource类型是System.Collections.IEnumerable。你绑定的是单个Model实例对象,无法转换。。所以要将它放进List里面。。不知道这样解释对不对,我是这么理解的,希望有用。。

sognoNL | 园豆:198 (初学一级) | 2012-07-17 15:06
0

ItemsSource绑定数据应该是list类型的 最好是ObservableCollection类型

While蹒跚学步... | 园豆:59 (初学一级) | 2013-03-01 09:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册