Datagrid绑定MVVM

时间:2014-05-20 20:47:41

标签: c# wpf xaml mvvm datagrid

我对MVVM很陌生,我只是试着用一个小的测试wpf应用程序弄湿我的脚。我有一个模型调用ToDoItemModel定义如下:(基本上我有一个名为ToDoItemModel的类和ToDoItemListModel实现IList

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();
    }
}

我想在我的视图中显示一个数据网格,该数据网格将通过ToDoListModelView绑定到ToDoListModel,该数据网格继承自ObservableObject。 ObservableObject实现了INotifyPropertyChanged

public class ToDoListModelView:ObservableObject
{
    ToDoListModel _myModel = new ToDoListModel();
    ...
}

我的视图由下面的xaml代码定义:基本上,我想将datagrid绑定到我的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之后,程序运行了,但我没有看到内容绑定到数据,尽管我在列表中至少有一个项目。我做错了什么?我知道它远非完整,但至少,我应该从数据网格中看到列表中的ToDoItem?我错过了什么? 请帮忙。谢谢!

----我的更新----------------------------------------- -----

ToDoListModel从具有返回基础数据的属性ToDoList的List继承

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"));
    }

为了简单起见,我还更正了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>

1 个答案:

答案 0 :(得分:1)

您将DataContext属性设置为“Model”类(您永远不应该做的事情,始终设置为ViewModel类),该类没有名为“ToDoListModelView”的属性,因此绑定失败(您应该在输出窗口中将其视为System.Data异常。)

ItemsSource="{Binding ToDoListModelView}"说“在我的DataContext上查找名为'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>

请注意,我将DataContext设置为ViewModel对象(不是模型),然后将其设置为窗口。这纯粹是文体风格,但通常不设置特定控件的DataContext属性(尽管有时需要)。然后我绑定到一个(虚构的)属性,该属性可能是ToDoListModel的实例。

其他一些旁白:

  1. 如果你继承IList,你可能做错了! ObservableCollection或其他现有实施之一有什么问题?
  2. 其“ViewModel”不是“ModelView”,仅作为命名惯例
  3. 类似但不重复(答案可以帮助您:Setting DataContext in XAML in WPF