'daki ListBox'a bağlanması, bu sorunla çok fazla saat harcadım.Gözlemlenebilir koleksiyonun XAML
class userClass : INotifyPropertyChanged
{
public int _key;
private string _fullName;
private string _nick;
public int key
{
get{return _key;}
set { _key = value; NotifyPropertyChanged("key"); }
}
private string nick
{
get { return _nick; }
set { _nick = value; NotifyPropertyChanged("nick"); }
}
private string fullName
{
get { return _fullName; }
set { _fullName = value; NotifyPropertyChanged("fullName"); }
}
public userClass()
{
nick = "nickname";
fullName = "fullname";
}
public userClass(String nick, String name, int key)
{
this.nick = nick;
this.fullName = name;
}
//INotifzPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public override string ToString()
{
return string.Format("{0} {1}, {2}", key, nick, fullName);
}
}
Ardından KullanıcıSınıfı sınıfının ObservableCollection bir sınıf olması:
I verilerle bir sınıf olması
class userListClass : ObservableCollection<userClass>
{
public userListClass(){}
//public override void Add(userClass user)
//{
// //user.PropertyChanged += new PropertyChangedEventHandler(user);
// base.Add(user);
//}
~userListClass()
{
//Serialize();
}
public void Serialize(ObservableCollection<userClass> usersColl)
{
FileStream fs = new FileStream("DataFile.dat", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(fs, usersColl);
}
catch (SerializationException e)
{
Console.WriteLine("Failed to serialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();
}
}
public void Deserialize()
{
FileStream fs = new FileStream("DataFile.dat", FileMode.Open);
try
{
BinaryFormatter formatter = new BinaryFormatter();
//users = (Hashtable) formatter.Deserialize(fs);
//usersColl = (ObservableCollection<userClass>)formatter.Deserialize(fs);
}
catch (SerializationException e)
{
MessageBox.Show(" Error: " + e.Message);
throw;
}
finally
{
fs.Close();
}
}
public override string ToString()
{
return "test";
//return base.ToString();
}
}
Aslında, kod, büyük bir kısmı, bir düzenleme test çok sonra seri hale getirme gibi çalışmıyor. Ama veri bağlama ve bağlama için gerekli olan şey şu anda çözdüğüm şey değil.
Bu koleksiyona sahibim ve listBox'a bağlamak istiyorum. Birkaç yolu denedim, ancak işe yaramadı.
Sen 2 şey yapmanız gerekenThe resource 'users' cannot be resolved.
<ListBox Grid.Column="0" Name="userViewLeft" ItemsSource="{Binding Source={StaticResource users} }" />
i sınıfını yapmaya çalışacaktı ve kişisel bir not – thumbmunkeys
kamu özelliklerini, ben size 'Sınıf' ile sınıf isimleri tüm soneki için yalvarıyorum –