WPF - 当我从其他窗口添加项目时,Observablecollection绑定DataGrid不更新

时间:2016-02-20 19:26:06

标签: c# wpf xaml mvvm datagrid

这是我第一次使用WPF。我已经看到很多问题,但无法得到正确的答案。场景是我有ObservableCollection的顺序由ViewModel实现。这是代码

namespace WpfApplication2.ViewModels
{
public class ViewModel : ObservableCollection<Order>
{
  static ObservableCollection<Order> orderlist = new ObservableCollection<Order>();

    public ParameterCommand ParameterCommand { get; set; }

    public ViewModel()
    {
        this.ParameterCommand = new ParameterCommand(this);
        for (int i = 0; i < 10; i++)
        {
             Order o = new Order();
             o.Imei = "Person"+ i;
             o.price = i*20;
             o.Cust_ID = i;


             Add(o);
        }

    }



    public void ParameterMethod(Order order)
    {
        Order o = new Order();
        o.Imei = order.Imei;
        o.price = order.price;
        o.Cust_ID = order.Cust_ID;


        Add(o);

    }
}
}

现在我有了一个主窗口,它具有由上面的viewmodel绑定的Datagrid MainWindow.xaml

 <Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:view ="clr-namespace:WpfApplication2"
    xmlns:model="clr-namespace:WpfApplication2.ViewModels"
    Title="MainWindow" Height="359.388" Width="630.791">
<Window.Resources>
    <view:Order x:Key="order"></view:Order>
    <model:ViewModel x:Key="modelview"/>  

    </Window.Resources>

<Grid DataContext="{Binding Source={StaticResource modelview}}" >
    <DataGrid HorizontalAlignment="Left"  AutoGenerateColumns="False"  Margin="48,26,0,0" DataContext="{Binding Source={StaticResource modelview}}" ItemsSource="{Binding}"  VerticalAlignment="Top" Height="235" Width="373">
        <DataGrid.Columns>
            <DataGridTextColumn  Header="Name"  Binding="{Binding Imei,Mode=TwoWay}"/>
            <DataGridTextColumn  Header="Cutomer ID"  Binding="{Binding Cust_ID,Mode=TwoWay}"/>
            <DataGridTextColumn  Header="Price"  Binding="{Binding price,Mode=TwoWay}"/>
        </DataGrid.Columns>
    </DataGrid>


    <TextBox HorizontalAlignment="Left" Height="23" DataContext="{Binding Source={StaticResource order}}" Margin="490,69,0,0" Text="{Binding Path=Imei, Mode=TwoWay}" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged_1"/>
    <TextBox HorizontalAlignment="Left" Height="23" DataContext="{Binding Source={StaticResource order}}" Margin="490,119,0,0" Text="{Binding Path=Cust_ID, Mode=TwoWay}" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <TextBox HorizontalAlignment="Left" Height="23" DataContext="{Binding Source={StaticResource order}}" Margin="490,165,0,0" Text="{Binding Path=price, Mode=TwoWay}" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>


    <Button Content="NEW" HorizontalAlignment="Left" Margin="109,276,0,0" VerticalAlignment="Top" Width="99" Command="{Binding ParameterCommand, Source={StaticResource modelview}}" CommandParameter="{Binding Source={StaticResource order}}"  Height="25"/>
    <Button Content="NEW" HorizontalAlignment="Left" Margin="239,276,0,0" VerticalAlignment="Top" Width="99" Height="25" Click="Button_Click_1"/>

</Grid>

现在当我将主窗口文本框中的值添加到ObservableCollection时,它会在datagrid中更新,但是当我使用其他单独的窗口在ObservableCollection中添加项时,它会被添加到其中但是datagrid不会更新。你能帮帮我吗? 修改 这是我向ObservableCollection添加项目的窗口的xaml      

    Title="New" Height="300" Width="300">

<Window.Resources>
    <view:Order x:Key="order"></view:Order>
    <model:ViewModel x:Key="modelview"/>

</Window.Resources>
<Grid>
    <TextBox HorizontalAlignment="Left" Text="{Binding Path=Imei, Mode=TwoWay}"  Height="23" Margin="83,30,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <TextBox HorizontalAlignment="Left" Text="{Binding Path=Cust_ID, Mode=TwoWay}" Height="23" Margin="83,85,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <TextBox HorizontalAlignment="Left" Text="{Binding Path=price, Mode=TwoWay}" Height="23" Margin="83,136,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <Button Content="Save to grid" HorizontalAlignment="Left" Margin="104,196,0,0" VerticalAlignment="Top" Width="75" Command="{Binding ParameterCommand, Source={StaticResource modelview}}" CommandParameter="{Binding Source={StaticResource order}}"/>

</Grid>

如果出现任何问题,也会附加命令

namespace WpfApplication2.ViewModels.Commands
{
  public class ParameterCommand : ICommand
  {

    public ViewModel ViewMod
    {
        get ;
        set ;
    }

    public ParameterCommand(ViewModel view)
    {
        this.ViewMod = view;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        this.ViewMod.ParameterMethod(parameter as Order);
    }
  }
}

ADDED 主窗口

public partial class MainWindow : Window
{
    public MainWindow()
    {

        InitializeComponent();
        //ViewModel vm = new ViewModel();
        //this.DataContext = vm;

    }


   private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        New win = new New();
        win .Owner = this;
        win .DataContext = this.DataContext;
        win .Show();
    }
}

1 个答案:

答案 0 :(得分:1)

这是因为您在新ViewModel中创建了window的新对象,并将数据用作DataContext。尝试以下代码:

Window w = new Window();
w.DataContext = this.DataContext;
w.Show();

当您打开新的window时,请使用当前DataContext实例作为新窗口的DataContext。并且您的代码可以正常工作。

<强>更新

从您New Window删除以下部分。

<Window.Resources>
    <view:Order x:Key="order"></view:Order>
    <model:ViewModel x:Key="modelview"/>
</Window.Resources>

使用<model:ViewModel x:Key="modelview"/>创建ViewModel New Window新对象的原因。

按以下方式更改Command

<Button Content="Save to grid" HorizontalAlignment="Left" Margin="104,196,0,0" VerticalAlignment="Top" Width="75" Command="{Binding ParameterCommand}" CommandParameter="{Binding Source={StaticResource order}}"/>
  

不确定您在CommandParameter尝试做什么。但是你的新人   窗口将现有ViewModel对象变为DataContext。   Command可以直接绑定到Button