WPF中关于ListBox绑定数据的问题

在定义ListBox的ItemSource绑定时,出现了初次绑定有数据,但是数据变化时视图没有更新的情况。
错误发生时代码如下:

//尝试一
private void binding(){
    List<string> m_allpaths = new List<string>();
    System.Windows.Data.Binding newBinding = new System.Windows.Data.Binding();
    newBinding.Source = m_allpaths;
    lsb1.SetBinding(System.Windows.Controls.ListBox.ItemsSourceProperty,newBinding);       
}
//尝试二,定义了一个类,此类中有个字段getAllPath为List<string>类型     

public class AllPaths : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private ObservableCollection<string> m_allpaths = new ObservableCollection<string>();
        public ObservableCollection<string> getAllPaths
        {
            get { return m_allpaths; }
            set
            {
                m_allpaths = value;
                if (PropertyChanged != null)
                {                    this.PropertyChanged.Invoke(this,new PropertyChangedEventArgs("getAllPaths"));
                }
            }
        }

    }
private void binding(){
    AllPath m_allpaths = new AllPath();
    System.Windows.Data.Binding newBinding = new System.Windows.Data.Binding();
    newBinding.Source = m_allpaths;
    newBinding.Path = new PropertyPath("getAllPaths");
    lsb1.SetBinding(System.Windows.Controls.ListBox.ItemsSourceProperty,newBinding); 
}     

发现尝试一中的m_allpaths和尝试二中的getAllPath进行添加时,ListBox的视图均为刷新。百度之后,了解到一个问题,就是对于List< T >这类泛型,视图层是不能够获取到其数据更新的。在comobox和listbox等空间中,其item都是多组字符,因此需要用到以下ObservableCollection< T >,msdn中有对ObservableCollection的介绍:http://msdn.microsoft.com/zh-cn/magazine/dd252944.aspx
只要将尝试二中的List< string >改成ObservableCollection< string >,即可绑定,也可以参照下面的代码:

private ObservableCollection<T> _default = new ObservableCollection<T>();
        public ObservableCollection<CardViewModel> DeafultPro
        {
            get { return _default; }
            set
            {
                if (_default != value)
                {
                    _default = value;
                    RaisePropertyChanged("DeafultPro");
                }

            }
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
WPF ListBox 绑定模板指的是在 ListBox 使用数据绑定绑定数据源,并使用自定义的模板来呈现数据。 以下是实现 WPF ListBox 绑定模板的步骤: 1. 创建数据源:创建一个集合类,用于存储数据源。 2. 绑定数据源:使用 ListBoxItemsSource 属性将数据绑定ListBox 控件上。 3. 创建数据模板:通过创建一个 DataTemplate 对象来定义自定义模板。 4. 应用模板:使用 ListBoxItemTemplate 属性将模板应用到 ListBox 控件上。 下面是一个示例代码,演示如何实现 WPF ListBox 绑定模板: ``` <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <ListBox ItemsSource="{Binding Students}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding Name}" /> <TextBlock Text="{Binding Age}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Window> ``` 在上述代码,我们创建了一个 ListBox 控件,并将它的 ItemsSource 属性绑定到一个名为 Students 的集合类上。然后,我们创建了一个 DataTemplate 对象,并在其定义了一个 StackPanel 和两个 TextBlock 控件,用于显示每个学生的姓名和年龄。最后,我们将模板应用到 ListBox ,以便呈现数据。 注意:在代码,我们使用了数据绑定和 MVVM 模式,这里不再赘述。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

话与山鬼听

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值