3

I have been trying to create a window in WPF and bind a listview to a list of objects, but I can't get the objects in the list to display in the listview.

Here is the XAML of the window:

<Window x:Class="WpfApplication.window_ManageInstruments"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="window_ManageInstruments" Height="400" Width="600" WindowStartupLocation="CenterOwner">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="100" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <ListView Grid.Row="1" Margin="5" Name="listInstruments" ItemsSource="{Binding InstrumentList}">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="120" Header="Symbol" DisplayMemberBinding="{Binding Field1}"/>
                <GridViewColumn Width="200" Header="Description" DisplayMemberBinding="{Binding Field2}"/>
                <GridViewColumn Width="120" Header="Last Stats" DisplayMemberBinding="{Binding Field3}"/>
                <GridViewColumn Width="100" Header="Margin" DisplayMemberBinding="{Binding Field4}"/>
            </GridView>
        </ListView.View>
    </ListView>

    <Label HorizontalAlignment="Left" Margin="10,10,0,0" Name="label1" Width="120" Height="25" VerticalAlignment="Top">Symbol</Label>
    <Label HorizontalAlignment="Left" Margin="10,40,0,0" Name="label2" Width="120" Height="25">Description</Label>
    <TextBox Height="25" Margin="150,10,0,0" Name="txtSymbol" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120" />
    <TextBox Margin="150,40,0,0" Name="txtDescription" HorizontalAlignment="Left" Width="120" Height="25" />
    <Button Height="23" HorizontalAlignment="Right" Margin="0,12,133,0" Name="btn_AddInstrument" VerticalAlignment="Top" Width="75">Add</Button>
</Grid>

and here is the code-behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;
using WpfApplication.classes;
using System.Collections.ObjectModel;

namespace WpfApplication {
    /// 
    /// Interaction logic for window_ManageInstruments.xaml
    /// 
    public partial class window_ManageInstruments : Window {
        public ObservableCollection InstrumentList = new ObservableCollection();

        public window_ManageInstruments() {
            this.DataContext = this;
            InstrumentList.Add(new TestClass { Field1 = "A", Field2 = "B", Field3 = "C", Field4 = "D" });
            InstrumentList.Add(new TestClass { Field1 = "E", Field2 = "F", Field3 = "G", Field4 = "H" });
            InitializeComponent();
        }
    }

    public class TestClass {
        public string Field1 { get; set; }
        public string Field2 { get; set; }
        public string Field3 { get; set; }
        public string Field4 { get; set; }
    }
}

I don't know if I am missing any crucial configuration here, so any help would be greatly appreciated.

Thank you in advance.

3 Answers 3

5

Your InstrumentList needs to be a property I think. Change it to

private ObservableCollection _instrumentList = new ObservableCollection();
public ObservableCollection InstrumentList{
  get{ return _instrumentList;}
}
2
  • Well that seems to do the trick. It's weird because I had already tried this before and did not work. Probably I made some changes in the meanwhile, including LueTm's answer. Thanks to both, I was about to start hammering my head against the wall.. Commented May 5, 2011 at 10:46
  • 1
    If you're having problems with bindings it's always a good idea to run your app up under the debugger and take a look at what's getting written to the Output window. There would probably have been a binding error telling you it could not find your list to bind to. Commented May 5, 2011 at 10:51
3

Does it work when you change the code to the following?

public window_ManageInstruments() 
{
    InitializeComponent();
    InstrumentList.Add(new TestClass { Field1 = "A", Field2 = "B", Field3 = "C", Field4 = "D" });
    InstrumentList.Add(new TestClass { Field1 = "E", Field2 = "F", Field3 = "G", Field4 = "H" });
    this.DataContext = this;
}
1
  • 1
    this.DataContext = this; was what was missing for me. Thanks
    – Viking
    Commented Dec 15, 2023 at 13:39
2

Change the InstrumentList declaration as a property.

public ObservableCollection<TestClass> InstrumentList { get; set; }

And add below line on Constructor

InstrumentList = new ObservableCollection<TestClass>();

It should work and collection will be binded to ListView.

Note - I am using generic collection here, you can change according to your need.

You can bind only to property not to variable.

1
  • Thank you for your answer. It achieves the same result as Russel's answer, without the separation between variable and property. I do, however, prefer the separation with a private variable and public getter / setter. Commented May 5, 2011 at 11:31

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.