2011-04-28 78 views
1

我想在我的WPF应用程序中使用MVVM。我目前有一个模型和一个有DataGrid和一些其他控件的视图。我创建了一个基于我的模型的ViewModel,不知道我是否做得正确。该视图只是一个简单的对话框。我想填充DataGrid视图。使用MVVM的WPF对话框

如何告诉DataGrid与ViewModel绑定?

我想绑定属性(像ID和日期之类的viewmodel内)到数据网格

所以,如果列表中有两个对象我想看到具有特定ID和日期的DataGrid中的两行。

我设置datacontext在类中,而不是xaml。

这是迄今为止代码:

public class ViewModel : INotifyPropertyChanged 
{ 
    private string _id; 
    private DateTime _date; 
    private ObservableCollection<Object> _list; 

    public string Id 
    { 
     get { return _id; } 
     set 
     { 
      _id = value; 
      PropertChanged("Id"); 
     } 
    } 
    public DateTime Date 
    { 
     get { return _date; } 
     set 
     { 
      _date = value; 
      PropertChanged("Date"); 
     } 
    } 
    public ObservableCollection<Object> list 
    { 
     get { return _list; } 
     set 
     { 
      _list = value; 
      PropertChanged("list"); 
     } 
    } 

    public LicenseViewModel() 
    { 
     list = GetList(); 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 

    public void PropertChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

而XAML:

<Window x:Class="Import" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit" 
     mc:Ignorable="d" 
     ResizeMode="CanResizeWithGrip" 
     x:Name="ImportLicense" 
     d:DesignHeight="493" d:DesignWidth="559" 
     Title="Import Licenses" SizeToContent="WidthAndHeight"> 

    <Grid Width="538"> 
     <DataGrid x:Name="Imported" VerticalAlignment="Top" AutoGenerateColumns="False" CanUserResizeColumns="True"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Header="Entitlement ID" Binding="{Binding Path=ID}"/> 
       <DataGridTextColumn Header="Date Sold" Binding="{Binding Path=Date}"/> 
      </DataGrid.Columns> 
     </DataGrid> 
    </Grid> 
</Window> 
+0

我把它用,而不是内部XAML与视图模型上背面itemsources解决。 – alice7 2011-04-29 15:18:42

回答

2

您需要为DataGrid的数据上下文设置为您的视图模型的实例。您可以通过在视图类的构造函数中将View或DataGrid的DataContext设置为您的视图模型的实例来完成此操作。这是一个快速而肮脏的做法。

如果你想成为更复杂的,你可以创建你的视图类DepenencyProperty这样的:

public static DependencyProperty ViewModelProperty = 
     DependencyProperty.Register("ViewModel", 
     typeof(ItemViewModel), 
     typeof(ViewClassHere)); 

public ItemViewModel ViewModel 
{ 
    get { return (ItemViewModel)base.GetValue(ItemViewModel); } 
    set { base.SetValue(ItemViewModel, value); } 
} 

然后你会绑定到该财产的许多方式中的任何一个,但一个办法是像:

<DataGrid ItemsSource="{Binding ElementName=windowName, Path=viewName.list}"> 

有很多方法可以做到这一点,这些只是两种可能的方式来做到这一点。

+0

我无法理解的一件事是viewModel如何与datagrid关联。以及viewmodel中的每个属性如何自动绑定到datagrid的列。我尝试了你的建议,但它对我没有用。 – alice7 2011-04-28 05:01:50

+0

让我们详细解释一下什么叫askin:datacontext将调用viewMOdel的构造函数,它将在内部填充list.BUt另外两个属性ID和Date。它们如何与数据网格绑定。 – alice7 2011-04-28 05:04:30

+0

@ alice7对不起,我没有正确地思考当你绑定到数据网格你想不绑定到你的视图的DataContext,但绑定ItemsSource,以便ItemsSource =“{绑定ElementName = windowName,路径= viewName.list }“ – 2011-04-28 22:24:47

0

有一个DataGrid来显示数据的常用的方法是设置的ItemsSource

<DataGrid ItemsSource="{Binding MyCollection}"/> 

您的视图模型定义2properties和一个集合,但在XAML中绑定你的属性到DataGrid列,不设置任何itemssource。

它对我来说不是很清楚你想在你的数据网格中看到什么,但是你的2个属性ID和DateTime不是任何集合的一部分,那么为什么你希望它显示在你的数据网格中?

请编辑你的问题,并提供一些你想在你的数据网格中看到的信息。

+0

我编辑了这个问题。抱歉不清楚我想要什么。我希望看到这些属性在viewmodel内的数据网格。 – alice7 2011-04-28 15:35:36

0

尝试这样的:

<window.Resources>    
<ViewModel x:Key="ViewModel"></ViewModel > 
</window.Resources> 
<Grid x:Name="ValueDetail" DataContext="{StaticResource ViewModel}"> 
<DataGrid ItemsSource="{Binding MyCollection}"/> 
</Grid>