CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2013
    Posts
    5

    Exclamation WPF Binding Problem with ObservableCollection

    I am having a problem binding a log file to a WPF list control.
    My list is ObservableCollection<string>

    My data changes through an even under the hood. My list should change in the window. I do not see changes. As my hardware issues events the data is updating the log file but the view is not updating.

    [Serializable]
    public class MeterLog : NotificationObject
    {
    private static MeterLog instance;
    public static MeterLog Instance
    {
    get
    {
    if(instance == null)
    instance = new MeterLog();
    return instance;
    }
    }

    private MeterLog()
    {
    Title = "Debug Log\n";
    Add(string.Format("Log Timesttamp:{0}", DateTime.Now.ToShortTimeString()));
    }

    public string Title { get; set; }
    private ObservableCollection<string> log = new ObservableCollection<string>();

    public ObservableCollection<string> Log
    {
    get { return log; }
    private set { log = value; this.RaisePropertyChanged("Log"); }
    }

    public void Add(string msg)
    {
    log.Add(msg);
    }

    public void WriteXMLFile()
    {
    MeterLog log = MeterLog.instance;

    System.Xml.Serialization.XmlSerializer writer =
    new System.Xml.Serialization.XmlSerializer(typeof(MeterLog));

    log.Title = "This is the Allication Error Log";

    System.IO.StreamWriter file = new System.IO.StreamWriter(
    AppDomain.CurrentDomain.BaseDirectory +"\\ApplicationLog.xml");

    writer.Serialize(file, log);
    file.Close();
    }
    }


    VIEW MODEL
    public ObservableCollection<string> ErrorLog
    {
    get
    {
    return myHardware.Log;
    }
    }

    My XAML is here

    <Window.DataContext>
    <vm:ViewModelOne />
    </Window.DataContext>

    <ListBox Margin="10,10,10,375" x:Name="lvErrors" ItemsSource="{Binding ErrorLog}" HorizontalAlignment="Stretch" Grid.Row="2" />

  2. #2
    Join Date
    May 2014
    Posts
    1

    Re: WPF Binding Problem with ObservableCollection

    You need to raise the property changed event for "ErrorLog" whenever the myHardware.Log gets updated.,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured