C# 绑定后的ObservableCollection更新
我有一个绑定到列表框的C# 绑定后的ObservableCollection更新,c#,wpf,observablecollection,C#,Wpf,Observablecollection,我有一个绑定到列表框的observeCollection。将集合作为列表的ItemsSource后,无法更新集合。如果我更新它,程序将关闭(没有任何崩溃) 代码: 我有一门课: class MyFile { String FileName {get; set;} ImageSource Ico {get; set;} } 然后在constractor中运行代码(在初始化Components之后) ObservableCollection filesList=新的Observab
observeCollection
。将集合作为列表的ItemsSource后,无法更新集合。如果我更新它,程序将关闭(没有任何崩溃)
代码:我有一门课:
class MyFile
{
String FileName {get; set;}
ImageSource Ico {get; set;}
}
然后在constractor中运行代码(在初始化Components之后)
ObservableCollection filesList=新的ObservableCollection();
添加(新的MyFile{Name=“bar.doc”,Ico=null}//工作正常
添加(新的MyFile{Name=“foo.txt”,Ico=null}//工作正常
files.ItemsSource=filesList;
添加(新的MyFile{Name=“try.txt”,Ico=null}//退出程序
我的程序出了什么问题
编辑只是用null代替GetIcon对它进行了测试。的任务是向侦听器宣布更改(添加、删除等)。因此,
ObservableCollection
没有问题,并将其绑定为ListBox的项源
,不会使其不可编辑。GetIcon
方法一定有问题(例如,尝试重新打开未正确关闭的同一图像)
要查看此想法是否正确,请尝试以下方法:
ObservableCollection<MyFile> filesList = new ObservableCollection<MyFile>();
filesList.Add(new MyFile { Name = "bar.doc", Ico = GetIcon(".doc") }
//filesList.Add(new MyFile { Name = "foo.txt", Ico = GetIcon(".txt") } // Comment this line
files.ItemsSource = filesList;
filesList.Add(new MyFile { Name = "try.txt", Ico = GetIcon(".txt") }
ObservableCollection filesList=新的ObservableCollection();
添加(新的MyFile{Name=“bar.doc”,Ico=GetIcon(“.doc”)}
//添加(新的MyFile{Name=“foo.txt”,Ico=GetIcon(“.txt”)}//注释此行
files.ItemsSource=filesList;
添加(新的MyFile{Name=“try.txt”,Ico=GetIcon(“.txt”)}
如果您想看到错误。我建议您在按钮的单击事件中复制这些行(而不是在构造函数中),然后运行您的应用程序。下面是一个关于使用DependencyProperty绑定ObservableCollection的小示例。我希望它能帮助您以这种方式进行数据绑定 XAML
代码隐藏
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
/// <summary>
/// Logica di interazione per MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static readonly DependencyProperty MyListProperty = DependencyProperty.Register("MyList", typeof(ObservableCollection<string>), typeof(MainWindow));
public ObservableCollection<string> MyList
{
get
{
return (ObservableCollection<string>)GetValue(MyListProperty);
}
set
{
SetValue(MyListProperty, value);
}
}
public MainWindow()
{
InitializeComponent();
MyList = new ObservableCollection<string>() { "a", "b" };
MyList.Add("c");
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用系统线程;
使用System.Collections.ObjectModel;
命名空间WpfApplication1
{
///
///每个MainWindow.xaml的内部逻辑
///
公共部分类主窗口:窗口
{
public static readonly dependencProperty MyListProperty=dependencProperty.Register(“MyList”、typeof(observeCollection)、typeof(main window));
公共可观测集合MyList
{
得到
{
返回(ObservableCollection)GetValue(MyListProperty);
}
设置
{
SetValue(MyListProperty,value);
}
}
公共主窗口()
{
初始化组件();
MyList=新的ObservableCollection(){“a”,“b”};
MyList.添加(“c”);
}
}
}
你能展示一下GetIcon
方法吗?它非常复杂,我是从互联网上得到的。如果我把null改为null,程序的行为也会很奇怪,那么文件的类型是什么?
只要列表框
带模板如果绑定在XAML上,那么“files.itemsource=filesList;”站在那里等待?我再次阅读文档并查看示例:要绑定对象,您应该使用DependencyProperty或实现InotyFyPropertyChanged。但是,再一次,请阅读文档!!!!至少在关于数据绑定的章节中,您了解了真正基本的内容。是的,我有图像和标签的项目模板。从co中删除这些行然后点击该按钮。这样你可以看到错误是什么。谢谢。我真正的observedcollection
是在另一个名为filemanager
的类中,它管理所有文件和它的单例类。我仍然喜欢我的方式,但是谢谢。请让我确定现在,您将如何在不正确使用绑定的情况下解决这个问题。看到一个人挑战WPF框架是非常有趣的。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="MyWindow"
Title="MainWindow">
<Grid>
<ListBox ItemsSource="{Binding MyList, ElementName=MyWindow, Mode=TwoWay}">
</ListBox>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
/// <summary>
/// Logica di interazione per MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static readonly DependencyProperty MyListProperty = DependencyProperty.Register("MyList", typeof(ObservableCollection<string>), typeof(MainWindow));
public ObservableCollection<string> MyList
{
get
{
return (ObservableCollection<string>)GetValue(MyListProperty);
}
set
{
SetValue(MyListProperty, value);
}
}
public MainWindow()
{
InitializeComponent();
MyList = new ObservableCollection<string>() { "a", "b" };
MyList.Add("c");
}
}
}