2011-10-07 10 views
7

में लिस्टबॉक्स में बाध्यकारी अवलोकन संग्रह मैंने इस समस्या के साथ कई घंटे बिताए हैं।एक्सएएमएल

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); 
    } 

} 

अगला मैं userClass वर्ग के ObservableCollection के साथ एक वर्ग है:

मैं डेटा के साथ एक वर्ग है

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(); 
    }  
} 

वास्तव में, एक संपादन कोड की, बड़ा हिस्सा परीक्षण के बहुत सारे के बाद serialization की तरह काम नहीं करता है। लेकिन डेटा बाध्यकारी और बाध्यकारी के लिए यह जरूरी नहीं है कि मैं अब क्या हल कर रहा हूं।

तो मेरे पास यह संग्रह है और इसे सूची बॉक्स में बांधना चाहते हैं। मैंने कई तरीकों की कोशिश की, लेकिन इसे काम करने के लिए नहीं मिला है।

The resource 'users' cannot be resolved.

<ListBox Grid.Column="0" Name="userViewLeft" ItemsSource="{Binding Source={StaticResource users} }" /> 
+3

, मैं और वर्ग बनाने की कोशिश करता हूँ यह सार्वजनिक – thumbmunkeys

+6

गुण है एक निजी नोट पर, मैं तुम्हें 'क्लास' के साथ अपने वर्ग के नाम के सभी प्रत्यय के लिए नहीं भीख माँगती हूँ –

उत्तर

26

कुछ बिंदुओं

ध्यान दिया जाना चाहिए
  • बनाओ गुण public और नहीं private
  • चर private करें।
  • नामकरण सम्मेलनों का पालन करें और कक्षा के पीछे class संलग्न न करें।
  • ItemsSource आप आपूर्ति डेटा के दायरे के अनुसार होना चाहिए, मेरे उदाहरण में कक्षा के दायरे में उपयोगकर्ता सूची और मैंने विंडो लोडेड ईवेंट पर आइटमसोर्स प्रदान किया है।

यहाँ एक पूर्ण उदाहरण कोड, इस में मैं ListBox अंदर ग्रिड नियंत्रण नेस्ट किया है क्योंकि बाद में आप VirtualizingStackPanel के लिए ListBox संपत्ति बदल सकता है। ताकि जब आपके पास सूची में भारी अपडेट हों तो इससे बड़ा प्रदर्शन लाभ मिलेगा। इसके अलावा आप BindingList का उपयोग कर सकते हैं जो मेरी राय में ObservableCollection से बेहतर है।

उपयोगकर्ता वर्ग:

public class User : INotifyPropertyChanged 
    { 
     private int _key; 
     private string _fullName; 
     private string _nick; 

     public int Key 
     { 
      get { return _key; } 
      set { _key = value; NotifyPropertyChanged("Key"); } 
     } 
     public string NickName 
     { 
      get { return _nick; } 
      set { _nick = value; NotifyPropertyChanged("NickName"); } 
     } 
     public string Name 
     { 
      get { return _fullName; } 
      set { _fullName = value; NotifyPropertyChanged("Name"); } 
     } 

     public User(String nick, String name, int key) 
     { 
      this.NickName = nick; 
      this.Name = name; 
      this.Key = key; 
     } 

     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, NickName, Name); 
     } 
    } 

उपयोगकर्ता सूची वर्ग:

public class Users : ObservableCollection<User> 
    { 
     public Users() 
     { 
      Add(new User("Jamy", "James Smith", Count)); 
      Add(new User("Mairy", "Mary Hayes", Count)); 
      Add(new User("Dairy", "Dary Wills", Count)); 
     } 
    } 

XAML:

<Grid> 
     <Button Content="Start" Height="23" HorizontalAlignment="Left" Margin="416,12,0,0" x:Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> 
     <ListBox x:Name="UserList" HorizontalContentAlignment="Stretch" Margin="12,41,12,12"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
         <Grid Margin="10"> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="20" /> 
           <ColumnDefinition Width="150" /> 
           <ColumnDefinition Width="*" /> 
          </Grid.ColumnDefinitions> 
          <TextBlock Text="{Binding Key}" Margin="3" Grid.Column="0" /> 
          <TextBlock Text="{Binding NickName}" Margin="3" Grid.Column="1" /> 
          <TextBlock Text="{Binding Name}" Margin="3" Grid.Column="2" /> 
         </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 

XAML कोड के पीछे:

public partial class MainWindow : Window 
{ 
    public static Users userslist = new Users(); 
    DispatcherTimer timer = new DispatcherTimer(); 

    public MainWindow() 
    { 
     InitializeComponent(); 
     this.Loaded += new RoutedEventHandler(MainWindow_Loaded); 
    } 

    void MainWindow_Loaded(object sender, RoutedEventArgs e) 
    { 
     timer.Interval = DateTime.Now.AddSeconds(10) - DateTime.Now; 
     timer.Tick += new EventHandler(timer_Tick); 
     UserList.ItemsSource = userslist; 
    } 

    void timer_Tick(object sender, EventArgs e) 
    { 
     userslist.Add(new User("Jamy - " + userslist.Count, "James Smith", userslist.Count)); 
     userslist.Add(new User("Mairy - " + userslist.Count, "Mary Hayes", userslist.Count)); 
     userslist.Add(new User("Dairy - " + userslist.Count, "Dary Wills", userslist.Count)); 
    } 

    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     if (button1.Content.ToString() == "Start") 
     { 
      button1.Content = "Stop"; 
      timer.Start(); 
     } 
     else 
     { 
      button1.Content = "Start"; 
      timer.Stop(); 
     } 
    } 

} 
एक पहला शॉट के रूप में
+2

आपको बहुत बहुत धन्यवाद। "UserList.ItemsSource = userslist;" और एक्सएएमएल कोड ने मुझे बहुत मदद की। – prespic

+1

मुझे पता है कि यह बहुत समय पहले था, लेकिन इससे मुझे बहुत मदद मिली है। इस तरह का एक संक्षिप्त जवाब। बाध्यकारी के संदर्भ में, 'Listbox' स्वयं लोड होने पर मुझे वर्तमान में एक त्रुटि मिल रही है, लेकिन मैंने आपके कोड की सहायता से इतनी सारी त्रुटियों के माध्यम से काम किया है। कुडोस! – plast1K

2

आप 2 काम करने होंगे:

पिछले एक मैंने कोशिश की मेरे लेखन त्रुटि दिया

सबसे पहले, जो भी तत्व (Window/UserControl/के DataContext निर्धारित कर सकते हैं) में आपके ListBox को एक ऑब्जेक्ट में शामिल किया गया है जो दिखता है:

public class ViewModel 
{ 
    public ViewModel() { this.users = new userListClass(); } 
    public userListClass users { get; private set; } 
} 

यह आपका व्यू मॉडल है, और यही वह है जिसे आप बांधना चाहते हैं।

दूसरा, ItemsSource="{Binding Path=users}" पर अपना बाध्यकारी बदलें। यह "this.DataContext पर संपत्ति users के मूल्य के मेरी ItemsSource संपत्ति के मूल्य सेट में तब्दील हो। क्योंकि DataContext माता-पिता से विरासत में मिला है, और आप ऊपर ViewModel वर्ग को यह निर्धारित करते हैं, अपने ListBox अब अपनी उपयोगकर्ता सूची प्रदर्शित करेगा।

संबंधित मुद्दे