2013-03-31 67 views
0

我有一个ObservbleCollection绑定到列表框。将该集合作为列表的ItemsSource后,我无法更新我的集合。如果我更新它,程序关闭(没有任何崩溃)。绑定后ObservableCollection更新

验证码:
我有类:

class MyFile 
{ 
    String FileName {get; set;} 
    ImageSource Ico {get; set;} 
} 

然后运行在constractor代码(InitializeComponents后)

ObservableCollection<MyFile> filesList = new ObservableCollection<MyFile>(); 
filesList.Add(new MyFile { Name = "bar.doc", Ico = null } // Work Fine 
filesList.Add(new MyFile { Name = "foo.txt", Ico = null } // Work Fine 
files.ItemsSource = filesList; 
filesList.Add(new MyFile { Name = "try.txt", Ico = null } // EXIT FROM PROGRAM 

什么是错在我的计划?

编辑
只是空测试它,而不是调用getIcon

+0

的背后,你能否告诉了'GetIcon'方法? –

+0

它非常复杂,我从互联网上得到它。如果我将把空值,而不是程序行为相同 – nrofis

+0

这很奇怪,“文件”的类型是什么? –

回答

0

ObservableCollection的使命是要宣布改变(添加,删除,...)给听者。因此,ObservableCollection没有什么问题,并将其作为ListBox的ItemsSource进行绑定,不会使其不可编辑。现在一定是错的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") } 

如果你想看到的错误。我建议你在按钮的单击事件中(而不是在构造函数中)复制这些行,然后运行你的应用程序。

+0

对不起,不行。即使我把null改为GetIcon – nrofis

+0

你在窗口的某个地方显示'Ico'吗? –

+0

是的,我有图像和标签的项目模板。 – nrofis

0

以下是使用DependencyPropery绑定ObservableCollection的小示例。我希望它能帮助你如何以这种方式进行DataBinding。

XAML

<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"); 
     } 
    } 
} 
+0

谢谢。我真正的'ObservableCollection'在另一个名为'FilesManager'的类中管理所有文件和它的单例类。我仍然喜欢我的方式,但谢谢。 – nrofis

+0

请让我知道你将如何摆脱这个问题,而不使用正确的绑定。看到一个挑战WPF框架的人很有意思。 –

相关问题