David Green
David Green

Reputation: 1234

Dynamically Adding Items To a WPF List View

I am trying to programatically add items to a ListView in WPF. I have done a lot of reading (including some questions here) and thought I was doing it correctly but the items aren't showing up. As I understand it I create the ListViewe and bind it to a data source, in this case an ObservableCollection. I have verified the ObservableCollection is getting items added to it, but they aren't getting displayed on the ListView. If it matters, the ListView is already instantiated by the time I run the LINQ query and attempt to add items to it.

Here is the XAML that defines the list view:

<TabPanel Name="ResultsTab" Height="200" Width ="500" DockPanel.Dock="Top" HorizontalAlignment="Left">
        <TabItem Name="Default_Tab" Header="Default">
            <ListView Name="DefaultListView" ItemsSource="Binding FCPortCollection">
                <ListView.View>
                    <GridView x:Name="DefaultGridView">
                        <GridViewColumn Width="Auto" Header="FC Port" DisplayMemberBinding="{Binding Path=FCPort}" />
                        <GridViewColumn Width="Auto" Header="WWPN" DisplayMemberBinding="{Binding Path=WWPN}"/>
                        <GridViewColumn Width="Auto" Header="FCID" DisplayMemberBinding="{Binding Path=FCID}" />
                        <GridViewColumn Width="Auto" Header="SwitchName" DisplayMemberBinding="{Binding Path=SwitchName}">
                        </GridViewColumn>                 
                    </GridView>   
                </ListView.View>     
            </ListView>
        </TabItem>

And here is the code that is supposed to load it.

public class PortResult
{
    public string SwitchName;
    public string FCPort;
    public string FCID;
    public string WWPN;

    public PortResult(string name, FCPort port)
    {
        SwitchName = name;
        FCPort = String.Format("fc{0}/{1}", port.SlotNum, port.PortNum);
        WWPN = port.WWPNList[0].WWPNValue;
        FCID = port.WWPNList[0].FCIDValue;

    }
}

ObservableCollection<PortResult> FCPortCollection = new ObservableCollection<PortResult>();

// results is an IEnumerable collection of FCPort result from a LINQ query that has been turned into a Dictionary
foreach (KeyValuePair<string, List<FCPort>> resultspair in results)
      {
            foreach (FCPort port in resultspair.Value)
            {    
                // create a new PortResult and add it to the ObservableCollection
                PortResult pr = new PortResult(resultspair.Key, port);
                FCPortCollection.Add(pr);
            }
        }

Upvotes: 0

Views: 4766

Answers (2)

ChrisWue
ChrisWue

Reputation: 19020

There are several problems in the code you posted:

  1. The binding syntax for your ItemsSource is missing the {} braces - it needs to be ItemsSource="{Binding FCPortCollection}"
  2. You can only bind to properties, however you only expose fields in your PortResult class. Change those fields to be properties.

Also make sure the DataContext of the ListView is set to the object which contains the FCPortCollection. Also make sure the collection is a property of the object and not a field (same reason as point 2. above).

Upvotes: 3

brunnerh
brunnerh

Reputation: 184376

This:

ItemsSource="Binding FCPortCollection"

Is not a binding, you forgot the braces {} and hence assigned a char[] as ItemsSource instead.

Upvotes: 2

Related Questions