0

I am using Caliburn.Micro and try to use MVVM; so my ViewModel and Views are separated; I've removed the xaml.cs from the View and run into an issue that my ListBox does not bind to a public ObservableCollection (And I am unable to figure out how)

Consider the following XAML:

            <Controls:Tile Title="Track &amp; Trace Reset"
               Controls:ControlsHelper.MouseOverBorderBrush="{DynamicResource MahApps.Brushes.ThemeForeground}"
               Width="155" Height="155"
               HorizontalTitleAlignment="Center"
               x:Name="Button" />
            <ListBox x:Name="LogEntries" ItemsSource="{Binding LogEntries}"/>

and the following class:

    public class DebugViewModel
    {
        public ObservableCollection<LogEntryModel> LogEntries = new ObservableCollection<LogEntryModel>(GlobalConfig.Connection.GetLogEntries());

        public void Button()
        {
            GetLogEntries();

        }

        private void GetLogEntries() {
            LogEntries = new ObservableCollection<LogEntryModel>(GlobalConfig.Connection.GetLogEntries());
            //if filter exists, filter the list
            return;
            //Format based on Severity

        }

    }

The "Button"-binding works as intended; the ListBox however doesn't display anything (I at least expected some raw-text from the Model that it should display). The LogEntries gets filled (8 entries) - that also functions;

How can I troubleshoot the binding issue?

Fimlore
  • 147
  • 1
  • 9

1 Answers1

1

LogEntries should be defined as a public property for the binding to work:

public ObservableCollection<LogEntryModel> LogEntries { get; } 
   = new ObservableCollection<LogEntryModel>(GlobalConfig.Connection.GetLogEntries());

Also note that using Caliburn.Micro, by convention, you could bind to a property in a corresponding view model by just setting the Name property in the view:

<ListBox x:Name="LogEntries" /> 
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thank you; that indeed gives me more to work with; Now it only complains about not being able to find a ViewModel for LogEntryModel; but that's an easy solution! – Fimlore May 25 '20 at 15:58