问题描述
您知道为什么要查看XAML和ViewModel中的代码,为什么在添加项目时数据网格不会更新吗?我知道DataGrid绑定是正确的,如果通过代码添加,它将添加一个项目,但是我是通过视图添加一个项目.
Do you know why looking at the code in the XAML and ViewModel why a Data Grid wont update when the an item is added? I know the DataGrid binding is correct as will add an item if I add it by code, however I am adding a item via a view.
每次添加项目时,ObservableCollection的吸气剂都会被击中.
Also the getter of the ObservableCollection is getting hit every time I do add a item.
谢谢
------代码------
------ Code ------
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)
VIEW 1 (that uses the XAML above)
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-传递值以添加到集合的视图
VIEW 2 - The view that passes the values to add to the collection
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();
}
}
VIEWMODEL
VIEWMODEL
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");
}
推荐答案
您在XAML中设置了DataContext
:
You set DataContext
in XAML:
<Window.DataContext>
<VM:ProductsViewModel />
</Window.DataContext>
,您的对话框将在您在代码中创建的ProductsViewModel
的另一个实例上工作:
and your dialog works on another instance of ProductsViewModel
which you create in code:
ProductsViewModel m_VM = new ProductsViewModel();
最简单的解决方法是将DataContext
传递到您的AddProductGroup
对话框:
Easiest workaround would be to pass DataContext
to your AddProductGroup
dialog:
AddProductGroup dlg = new AddProductGroup(this.DataContext as ProductsViewModel);
编辑
或在代码中设置它,而不是在XAML中设置DataContext
:
or instead of setting DataContext
in XAML do set it in code:
public MainWindow()
{
InitializeComponent();
this.DataContext = m_VM;
}
这篇关于数据网格未从ViewModel更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!