简体   繁体   中英

WPF - Observablecollection binded DataGrid not updating when i add item from other window

Its my first time with WPF. I already seen many questions but couldn't get the right answer. Scenario is that I have ObservableCollection of order which is implemented by ViewModel. Here is the code

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);

    }
}
}

Now I have the Main Window which have the Datagrid binded by above viewmodel 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>

now when i add the values from main window text boxes to ObservableCollection it is updated in the datagrid but when i use other separate window for adding item in ObservableCollection it got added in it but datagrid is not updated. Can you please help me out EDIT: here is the xaml of the window I am adding item to ObservableCollection

    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>

Commands also attaching in case any problem is there

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 Main Window.cs

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();
    }
}

It's cause you are creating new object of ViewModel in new window and using data as DataContext . try below code:

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

When you open the new window , use current DataContext instance as new window's DataContext . And your code will work fine.

Update

Remove below part from you New Window .

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

Cause of <model:ViewModel x:Key="modelview"/> a new object of ViewModel is being created with the New Window .

Change your Command as following:

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

Not sure what are you trying to do in CommandParameter . But your New Window is getting existing ViewModel object as DataContext . Command can be directly bound to Button .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM