0

today I've been struggling with proper binding and it seems like i can't figure out what is wrong with my approach. I have one basic window with ListView as it's main component. When I try to bind ListView's ItemSource to the ObservableCollection<> through XML, it seems like it doesn't work, but if I change the property through C# code, it works just fine. My guess would be it has something to do with visibility of the ObservableCollection<>, but whenever i try to make it public i get error that base class is less visible than the other class. Could anyone explain what am I missing here?

public partial class MainWindow : Window
    {
        public ObservableCollection<dataModel> items;
        public MainWindow()
        {
            items = new ObservableCollection<dataModel>();
            
            
            InitializeComponent();
            populateData();

            //LV1.ItemsSource = items; <= works with this uncommented
        }

and XAML:

<ListView x:Name="LV1" HorizontalAlignment="Left" Height="264" Margin="55,59,0,0" VerticalAlignment="Top" Width="552" ItemsSource="{Binding items}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="title" Width="200" DisplayMemberBinding="{Binding title}"></GridViewColumn>
                    
                </GridView>
            </ListView.View>
        </ListView>

dataModel.cs:

public class dataModel
    {
        public string title { get; set;}
        public bool isDone {get;set;}

      
    }
vastyr
  • 1
  • 2
  • 1
    You need public properties to bind. It's also not clear that you've set your datacontext. – Andy Dec 28 '22 at 16:12
  • 1
    Also. If you only have one thing to show then you can use a listbox rather than listview. Listview is used for multi column data. Listview is a sort of a lighter weight datagrid. Way back in the day, there was no datagrid in the wpf toolbox. They just had listview. That is a bit low on functionality so they added the datagrid. – Andy Dec 28 '22 at 16:15
  • @Andy - No, i don't seem to set datacontext as it wasn't really shown anywhere i looked (MSDN guides and many answers on stack). Though adding it just above the commented ItemsSource (DataContext = this;) it still doesn't show anything. As for the data - i have public class with 2 public fields (one string and one bool), that's why i went with listview. – vastyr Dec 29 '22 at 08:32
  • Ike I said. Fields still won't bind. You still need a public property to bind. – Andy Dec 29 '22 at 10:13
  • I've updated the question with dataModel.cs. I really appreciate the help you're providing, but Im kind of lost at "you still need a public property to bind". Both observableCollection items and the model itself are public. What do you mean by public property? – vastyr Dec 29 '22 at 14:24
  • Nevermind, I've figured out the difference and made it work now - thanks for your explaination, I've learned a lot thanks to you. – vastyr Dec 29 '22 at 14:40

0 Answers0