数据网格不从ViewModel更新

时间:2014-04-05 15:21:00

标签: wpf xaml mvvm observablecollection

您知道为什么要查看XAML和ViewModel中的代码,为什么数据网格在添加项目时不会更新?我知道DataGrid绑定是正确的,因为如果我按代码添加它会添加一个项目,但是我通过视图添加项目。

每次添加项目时,ObservableCollection的getter都会被点击。

由于

------代码------

XAML

<Window x:Class="Importer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Product Group" Height="350" Width="721"
        xmlns:VM="clr-namespace:Importer.ViewModels">
    <Window.DataContext>
        <VM:ProductsViewModel />
    </Window.DataContext>
    <Grid Margin="0,0,0.4,0.4">
        <DataGrid x:Name="dgGrid" ColumnWidth="Auto" Margin="10,10,0,0"  VerticalAlignment="Top" 
                  ItemsSource="{Binding ProductCollection}" HorizontalAlignment="Left" AutoGenerateColumns="False" >
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="150"/>
                <DataGridTextColumn Binding="{Binding Description}" Header="Description" Width="400" />
            </DataGrid.Columns>
        </DataGrid>
        <Button x:Name="btnAddProjectGroup" Content="Add Project Group" HorizontalAlignment="Left" Margin="10,277,0,0" VerticalAlignment="Top" Width="135" Click="btnAddProjectGroup_Click"/>
    </Grid>
</Window>

VIEW 1(使用上面的XAML)

 public partial class MainWindow : Window
   {
        ProductsViewModel m_VM = new ProductsViewModel();

        public MainWindow()
        {     
            InitializeComponent();
        }


      private void btnAddProjectGroup_Click(object sender, RoutedEventArgs e)
      {
       // Open new window here to add a new project group
          AddProductGroup dlg = new AddProductGroup(m_VM);

         dlg.ShowDialog();
      }
   }

视图2 - 传递值以添加到集合

的视图
public partial class AddProductGroup : Window
    {
        ProductGroupBindable newProduct = new ProductGroupBindable();
        ProductsViewModel viewModel = null;

        public AddProductGroup(ProductsViewModel vm)
        {
            viewModel = vm;

            InitializeComponent();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            newProduct.Id = System.Guid.NewGuid();
            newProduct.Name = txtName.Text;
            newProduct.Description = txtDescription.Text;

            viewModel.AddProduct(newProduct);
            this.Close();
        }
    }

视图模型

 private ObservableCollection<ProductGroupBindable> m_ProductCollection;


        public ProductsViewModel()
        {
            if (m_ProductCollection == null)
                m_ProductCollection = new ObservableCollection<ProductGroupBindable>();
        }


     public ObservableCollection<ProductGroupBindable> ProductCollection
        {

            get
            {
                return m_ProductCollection;
            }
            set
            {
                m_ProductCollection = value;
            }

        }

        private ObservableCollection<ProductGroupBindable> Test()
        {
            m_ProductCollection.Add(new ProductGroupBindable { Description = "etc", Name = "test12" });
            m_ProductCollection.Add(new ProductGroupBindable { Description = "etc", Name = "test123" });

            return ProductCollection;
        }

        public void AddProduct(ProductGroupBindable newProduct)
        {
            m_ProductCollection.Add(newProduct);
            //NotifyPropertyChanged("ProductCollection");
        }

1 个答案:

答案 0 :(得分:1)

您在XAML中设置DataContext

<Window.DataContext>
    <VM:ProductsViewModel />
</Window.DataContext>

并且您的对话框适用于您在代码中创建的ProductsViewModel的另一个实例:

ProductsViewModel m_VM = new ProductsViewModel();

最简单的解决方法是将DataContext传递到您的AddProductGroup对话框:

AddProductGroup dlg = new AddProductGroup(this.DataContext as ProductsViewModel);

修改

或者不是在XAML中设置DataContext,而是在代码中设置它:

public MainWindow()
{     
    InitializeComponent();
    this.DataContext = m_VM;
}