简体   繁体   中英

Wpf Listview not displaying data

I have a Model as following.. I dont want to do anything like INotify etc because nothing changes in the view, I just want to display the names in the View. And in the view model Iam exposing model property because in mvvm View should bind to the view model only. But for some reason the names are not being displayed..Please help..

namespace WpfApplication4
{
    public class Model
    {
        private string _name;
        public string name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

    }

    public class make
    {
        List<Model> mod = new List<Model>();
        public make()
        {
            mod.Add(new Model { name = "Sam" });
            mod.Add(new Model { name = "Pam" });
            mod.Add(new Model { name = "Jam" });
            mod.Add(new Model { name = "Cam" });
        }

        public List<Model> modlist()
        {
            return mod;
        }
    }
}

View Model:

namespace WpfApplication4
{
    public class ViewModel
    {
        private ObservableCollection<Model> _obcollection;
        private Model _Model;
        public Model model
        {
            get
            {
                return _Model;
            }
            set
            {
                _Model = value;
            }
        }

        public ViewModel()
        {
            Convert();
        }



        public void Convert()
        {
            make m = new make();
            _obcollection = new ObservableCollection<Model>(m.modlist());
        }

        public ObservableCollection<Model> obcollection
        {
            get
            {
                return _obcollection;
            }
            set
            {
                _obcollection = value;
            }
        }
    }
}

And Xaml for Main Window.:

<Grid>
        <ListView Name="lstdemo" ItemsSource="{Binding obcollection}">
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=model.name}" Header="Name" />
                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>

And finally MainWindow.cs

public MainWindow()
        {
            InitializeComponent();
            ViewModel vm = new ViewModel();
            this.DataContext = vm;
        }

I believe your xaml should be following:

<Grid>
    <ListView Name="lstdemo" ItemsSource="{Binding obcollection}">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=name}" Header="Name" />
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

Each item is already bound to a model.
Instead of DisplayMemberBinding="{Binding Path=model.name}" use DisplayMemberBinding="{Binding name}"

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