【问题标题】:Data Grid not updating from ViewModel数据网格未从 ViewModel 更新
【发布时间】:2014-04-05 15:20:59
【问题描述】:

您知道为什么要查看 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();
      }
   }

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

【问题讨论】:

  • 你在btnAddProjectGroup_Click做什么?
  • 我将编辑帖子以显示我的 2 个视图

标签: wpf xaml mvvm observablecollection


【解决方案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;
}

【讨论】:

  • 谢谢 dkozl,请问这是“黑客”还是被认为是 MVVM 的工作方式。我只工作了 1 天,所以我热衷于尝试从一开始就尽可能多地使用 MVVC 方式。再次感谢
  • 在代码中初始化DataContext是完全可以接受的
  • 谢谢你,我知道你没有因为这个帮助而得到报酬,但如果你确实看到我可以做的任何不同的事情(特别是在 ViewModel 中)你能告诉我吗?但是,我使用了 AddProductGroup dlg = new AddProductGroup(this.DataContext as ProductsViewModel);对于解决方案,再次欢呼!
  • 如果没有 MVVM LightCaliburn.Micro 这样的框架,则需要额外的工作才能做到这一点,但是如果您想学习 MVVM,请尝试自己编写。
  • WPF 基于几个基本接口,例如用于视图模型的INotifyPropertyChanged、用于集合的INofityCollectionChangedObservableCollection 实现了该接口)和用于命令绑定的ICommand。查看命令绑定。例如Button 具有Command,您可以绑定到视图模型中的一些ICommand 以执行某些操作。您可以像绑定其他属性一样在 WPF 中绑定命令
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-08
  • 2014-10-18
  • 2017-02-13
  • 2020-12-31
相关资源
最近更新 更多