Reputation: 1736
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;
}
Upvotes: 1
Views: 4942
Reputation: 608
Each item is already bound to a model.
Instead of DisplayMemberBinding="{Binding Path=model.name}"
use DisplayMemberBinding="{Binding name}"
Upvotes: 0
Reputation: 192
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>
Upvotes: 1