简体   繁体   中英

ListView Observable Collection Will Not Show Members

Cannot get my listview to display data.

XAML

<Grid>
    <DockPanel>
        <ListView Name="lstDetectedComputers" 
                  MinWidth="200" 
                  DockPanel.Dock="Left" 
                  ItemsSource="{Binding ComputersList}" DisplayMemberPath="ComputerName">
        </ListView>
        <DataGrid x:Name="ViewNetworkCardInformation" 
                  ItemsSource="{Binding NetworkCardInformation}"/>
    </DockPanel>
</Grid>

Code:

private ObservableCollection<Object> _ComputersList;
public ObservableCollection<Object> ComputersList
{
    get 
    { 
        return _ComputersList;
    }

    set 
    {
        _ComputersList = value; NotifyPropertyChanged("ComputersList");
    }
}

private DataTable _NetworkCardInformation;
public DataTable NetworkCardInformation
{
    get 
    { 
        return _NetworkCardInformation; 
    }

    set 
    { 
        _NetworkCardInformation = value; NotifyPropertyChanged("NetworkCardInformation"); 
    }
}

Strange thing is that NetworkCardInformation shows in my datagrid so this indicates that the datacontext is working as expected.

now im under the impression with a ObservableCollection i do not need a INotifyPropertyChange, if this is wrong please advised.

i have also tried just ItemsSource="{Binding ComputersList}"

I have put a break point into the code to ensure that the observable collection has data, and it is there .

  • ComputersList Count = 2 System.Collections.ObjectModel.ObservableCollection
  • [0] {AdminUltimate.Model.NetworkModel.ComputerNode} object {AdminUltimate.Model.NetworkModel.ComputerNode} ComputerName "ASUS-PC" string

Could someone please assist. Thank you

You have set DisplayMemberPath as ComputerName but Object doesn't have any such property so it shows nothing on view.

This can be validated by removing DisplayMemberPath , you will see fully qualified class name of your object since ToString() gets called on your object if no ItemTemplate and DisplayMemberPath is set on ListBox.

So, solution would be to change ObservableCollection<Object> to type of more concrete object containing property ComputerName ie ObservableCollection<ComputerNode> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM