我有一个如下的模型..我不想做任何像INotify等的事情,因为视图中没有任何变化,我只想在视图中显示名称。并且在视图模型中我暴露了模型属性,因为在mvvm中View应该只绑定到视图模型。但由于某种原因,这些名字没有显示出来..请帮助..
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;
}
}
}
查看型号:
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;
}
}
}
}
主窗口的Xaml。:
<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>
最后是MainWindow.cs
public MainWindow()
{
InitializeComponent();
ViewModel vm = new ViewModel();
this.DataContext = vm;
}
答案 0 :(得分:1)
我相信你的xaml应该遵循:
<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>
答案 1 :(得分:0)
每个项目都已绑定到模型
而不是DisplayMemberBinding="{Binding Path=model.name}"
使用DisplayMemberBinding="{Binding name}"