C# 数据网格绑定MVVM
我是MVVM的新手,我只是想用一个小的wpf应用程序来测试一下。我有一个模型调用ToDoItemModel,定义如下:(基本上,我有一个名为ToDoItemModel和ToDoItemListModel的类,它实现了IListC# 数据网格绑定MVVM,c#,wpf,xaml,mvvm,datagrid,C#,Wpf,Xaml,Mvvm,Datagrid,我是MVVM的新手,我只是想用一个小的wpf应用程序来测试一下。我有一个模型调用ToDoItemModel,定义如下:(基本上,我有一个名为ToDoItemModel和ToDoItemListModel的类,它实现了IList public class ToDoItemModel { private DateTime _TodoDate; private string _TodoDescription; private TimeSpan _TodoT
public class ToDoItemModel
{
private DateTime _TodoDate;
private string _TodoDescription;
private TimeSpan _TodoTimeSpan;
private string _StartTime;
public ToDoItemModel(DateTime d, string s)
{
_TodoDate=d;
_TodoDescription=s;
}
public string StartTime
{
get { return _StartTime; }
set { _StartTime = value; }
}
public TimeSpan ToDoTimeSpan
{
get { return _TodoTimeSpan; }
set { _TodoTimeSpan = value; }
}
public string ToDoDescription
{
get { return _TodoDescription; }
set { _TodoDescription = value; }
}
public DateTime ToDoDate
{
get { return _TodoDate; }
set { _TodoDate = value; }
}
public override string ToString()
{
return string.Format("Date: {0}- Time: {1}- Duration: {2}- Description: {3}",_TodoDate.ToString("d"),_StartTime,_TodoTimeSpan,_TodoDescription);
}
}
public class ToDoListModel: IList<ToDoItemModel>
{
private readonly IList<ToDoItemModel> _todoList = new List<ToDoItemModel>();
public ToDoListModel()
{
//Hard code this one for testing purpose
_todoList.Add(new ToDoItemModel(DateTime.Now,"Testing"));
}
public int IndexOf(ToDoItemModel item)
{
return _todoList.IndexOf(item);
}
public void Insert(int index, ToDoItemModel item)
{
_todoList.Insert(index,item);
}
public void RemoveAt(int index)
{
_todoList.RemoveAt(index);
}
public ToDoItemModel this[int index]
{
get
{
return _todoList[index];
}
set
{
_todoList[index]=value;
}
}
public void Add(ToDoItemModel item)
{
_todoList.Add(item);
}
public void Clear()
{
_todoList.Clear();
}
public bool Contains(ToDoItemModel item)
{
return _todoList.Contains(item);
}
public void CopyTo(ToDoItemModel[] array, int arrayIndex)
{
_todoList.CopyTo(array,arrayIndex);
}
public int Count
{
get { return _todoList.Count; }
}
public bool IsReadOnly
{
get { return _todoList.IsReadOnly; }
}
public bool Remove(ToDoItemModel item)
{
return _todoList.Remove(item);
}
public IEnumerator<ToDoItemModel> GetEnumerator()
{
return _todoList.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
我的视图由下面的xaml代码定义:基本上,我希望将该数据网格绑定到我的ToDoListModel
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestWPF" x:Class="TestWPF.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Grid Margin="1,0,-1,0">
<DataGrid x:Name="ExistingToDoList"
HorizontalAlignment="Left"
Margin="33,201,0,0"
VerticalAlignment="Top"
Height="94" Width="464"
Background="#FFF1E6E6"
GridLinesVisibility="Horizontal"
ItemsSource="{Binding ToDoListModelView}"
Style="{DynamicResource ToDoEntry}">
<DataGrid.DataContext>
<local:ToDoListModel/>
</DataGrid.DataContext>
</DataGrid>
</Grid>
</Window>
然而,在点击F5后,程序运行了,但我没有看到绑定到数据的内容,尽管我在列表中至少有一项。我做了什么不正确?我知道它远没有完成,但至少,我应该从datagrid中看到列表中的ToDoItem?我错过了什么?
请帮忙,谢谢
----我的更新----------------------------------------------
ToDoListModel继承自具有返回基础数据的属性ToDoList的列表
public class ToDoListModel: List<ToDoItemModel>
{
private List<ToDoItemModel> _todoList = new List<ToDoItemModel>();
public List<ToDoItemModel> TodoList
{
get { return _todoList; }
}
public ToDoListModel()
{
_todoList.Add(new ToDoItemModel(DateTime.Now,"Testing"));
}
公共类ToDoListModel:列表
{
私有列表_todoList=新列表();
公开名单
{
获取{return\u todoList;}
}
公共ToDoListModel()
{
_添加(新的ToDoItemModel(DateTime.Now,“Testing”);
}
我还修改了datagrid,将其绑定到ToDoList,并将autoGeneratecolumn设置为true,以简化说明
<Grid Margin="1,0,-1,0" d:DataContext="{d:DesignData /SampleData/ToDoListModelSampleData.xaml}">
<DataGrid x:Name="ExistingToDoList"
HorizontalAlignment="Left"
Margin="33,201,0,0"
VerticalAlignment="Top"
Height="94" Width="464"
Background="#FFF1E6E6"
GridLinesVisibility="Horizontal"
ItemsSource="{Binding TodoList}"
Style="{DynamicResource ToDoEntry}"
AutoGenerateColumns="True"
DataContext="{Binding Mode=OneWay}">
<DataGrid.ItemBindingGroup>
<BindingGroup/>
</DataGrid.ItemBindingGroup>
</DataGrid>
您将
DataContext
属性设置为“Model”类(您永远不应该这样做,总是设置为ViewModel类),该类没有名为“ToDoListModelView”的属性,因此绑定失败(您应该在输出窗口中将其视为System.Data异常)
编写ItemsSource=“{Binding ToDoListModelView}”
时说“在我的数据上下文中查找名为“ToDoListModelView”的属性,并将其用作我的ItemsSource”。您可能打算做的是:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestWPF" x:Class="TestWPF.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:ToDoListModelView/>
</Window.DataContext>
<Grid Margin="1,0,-1,0">
<DataGrid x:Name="ExistingToDoList"
ItemsSource="{Binding MyModelProperty}"
</DataGrid>
</Grid>
</Window>
根据您提供的内容,您似乎没有将网格绑定到IEnumerable,而是绑定到包含IEnumerable的类。感谢您指出我的代码中存在的问题。对于这种情况,ILIST无疑是一种过火的做法。我已将其更改为LIST和命名约定。我对此完全没有兴趣。我所做的关于is ItemsSource binding:是否应该将其绑定到一行或整个类?假设我将类更改为OlistModel:List,其中包含返回整个列表的属性ToDoList和一个属性ToDoList(索引)。当我绑定itemsource时,我应该将其绑定到ToDoList还是ToDoList?我必须定义datagrid中的每个字段吗?您绑定到“ToDoList”(ItemsSource始终绑定到集合),是的,您需要定义每个字段(AutoGenerateColumns可以工作,但您几乎总是希望自定义它)。这有意义吗?是的,应该有意义。我完全理解你的解释。但是,在应用程序启动时,它仍然没有显示数据。它在设计时显示列,但在运行时没有显示。我想知道我遗漏了什么?我已经更新了我更改的部分,以便在我的question@user1205746您的DataContext是否已设置到ToDoListModel?这仍然是关闭的,您也不应该从列表派生。为什么不使用一个只拥有列表的ViewModel类呢?这仍然是以一种非常有趣的方式设置的。啊!DataContext!!!我错过了…难怪!是的,我是新来的,所以我正在尝试让我的脚湿起来,所以有很多事情是不符合规则的。谢谢您指出它出去。非常感谢!
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestWPF" x:Class="TestWPF.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:ToDoListModelView/>
</Window.DataContext>
<Grid Margin="1,0,-1,0">
<DataGrid x:Name="ExistingToDoList"
ItemsSource="{Binding MyModelProperty}"
</DataGrid>
</Grid>
</Window>