Click here to Skip to main content
15,438,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Explain why the method works so crooked add to the collection and display DataGrid.Ispolzuyu binding in XAML.
XML
<Window x:Class="bild.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:bild"
        Title="MainWindow" Height="519" Width="525">
    <Window.Resources>
        <sys:ViewModel x:Key="DataSource"/>        
    </Window.Resources>
 
        <Grid>
        <ListBox ItemsSource="{Binding Source={StaticResource DataSource}, Path=Persons}" 
                 DisplayMemberPath="Name"                 
            HorizontalAlignment="Left" Margin="49,21,0,201" Name="listBox1" Width="425" />
        <Grid DataContext="{Binding Source={StaticResource DataSource}}" Height="149" HorizontalAlignment="Left" Margin="49,134,0,0" Name="grid1" VerticalAlignment="Top" Width="425">
            <DataGrid 
                ItemsSource="{Binding Source={StaticResource DataSource}, Path=Persons}" 
                AutoGenerateColumns="False" Height="143" HorizontalAlignment="Left" Margin="36,0,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="383" >
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Age}"  Header="Age" ></DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="296,290,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>


C#
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace bild
{   
    public partial class MainWindow : Window
    {
        
        public MainWindow()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ViewModel r = new ViewModel();
            r.ad();
            dataGrid1.ItemsSource = r.Persons;
        }
    }
    public class ViewModel
    {
        public ViewModel()
        {
            this.Persons = new ObservableCollection<Person>();
            this.Persons.Add(new Person("Ivan", 23));
            this.Persons.Add(new Person("Stefan", 34));
            this.Persons.Add(new Person("Maria", 16));
            this.Persons.Add(new Person("Michael", 78));
            
        }
        public void ad()
        {
           this.Persons = new ObservableCollection<Person>();
            this.Persons.Add(new Person("Maria", 16));
            this.Persons.Add(new Person("Michael", 78));
          
        }
 
        public ObservableCollection<Person> Persons
        {
            get;
            set;
        }
    }
    public class Person
    {
        public Person(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
    }
}
Posted
Comments
CHill60 9-Feb-13 17:42pm    
Your question is not clear. Try rewording it

1 solution

I don't understand what you tried to do but in the add method. Seems you recreate the Persons collection.
Also you call the data grid items source but not the list so the list doesn't change.
Note: You shouldn't call the data grid items source but implement the INotifyPropertyChanged and fire an event that the Persons collection changed.

Something like this:

public class ViewModel : INotifyPropertyChanged
{
....

public void Ad()
{
this.Persons.Add(...
OnPropertyChanged("Persons");
}

public event PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged(string propName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventHandler(propName));
}
}

This way both the data grid and the list will be updated.
 
Share this answer
 
v2
Comments
Sawyer1988 10-Feb-13 10:27am    
Thank you!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900