Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi together,

i know that this is kind of a regular question but i am konfused on the behavior of my programm. I want to bind an observablecollection of custom objects to a datagrid. When the binding is done via code, after receiving the list over wcf, the datagrid shows the itemssource. But this doesn't work when i bind over xaml. i thought, that the observable collection automaticaly updates the datagrid over the collectionchangedevent. Here is my Code with a few comments:

C#
public partial class AddressPage : Page
    {
        ObservableCollection<Address> AddressCollection =
        new ObservableCollection<Address>();
    
        public AddressPage()
        {
            this.LoadAddress();
            InitializeComponent();
        }

        private async void LoadAddress()
        {
            RestServiceClient client = new RestServiceClient();
            AddressCollection = await client.GetAddressAsync();
            MainGrid.ItemsSource = AddressCollection; //this works but i dont want it this way...
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            AddressCollection.Add(
            new Address { address1 = "test1", address2 = "test2" });
        }
    }


This is the XAML Code. The ItemsSource="{Binding AddressCollection}" is not working...

XML
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Style="{StaticResource standardDataPage}"
d:DesignHeight="300" d:DesignWidth="300"
Title="AddressPage">

XML
<StackPanel>
<DataGrid x:Name="MainGrid" ItemsSource="{Binding AddressCollection}"  AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="address1" Binding="{Binding address1}"/>


Beside a few posts i read the comarison-articel:
List vs ObservableCollection vs INotifyPropertyChanged in Silverlight[^]
and the tutroial:
WPF DataGrid Practical Examples[^]
But i cant't get it...
Posted

1 solution

Hi,

" i thought, that the observable collection automaticaly updates the datagrid over the collectionchangedevent" -this is a first confusion
this event will fire in oly one case when you (add, delete .. etc data whithin this collection).
So
MainGrid.ItemsSource = AddressCollection;
doesn't fire the event!!

If you want all this stuff work automatically withot any code like below, you need, to provide MVVM pattern to your Data

for example:
C#
public class MainViewModel
{
private ObservableCollection<address> _addressCollection;
public ObservableCollection<address> AddressCollection
{
  get{ return _addressCollection;}
  set{ _addressCollection=value; RaiseProperchyChanged("AddressCollection")}
} 
}
</address></address>

XML
<datagrid x:name="MainGrid" itemssource="{Binding AddressCollection}" datacontext="{Binding" mode="hold" xmlns:x="#unknown" />
 
Share this answer
 
Comments
ChrisTopherus 27-Dec-12 9:16am    
Hi Oleksandr, thank you for this answer. I tried to fiddle your code into mine but i do not have the "RaisePropertyChanged" event.
And because of time, i am not able to learn the mvvm model at the moment... and i cant use the binding in the code, because i need to autogenerate a lot of the xaml-pages.
Oleksandr Kulchytskyi 27-Dec-12 9:20am    
As concerns RaisePropertyChanged read this http://wilberbeast.com/2010/07/21/wpf-mvvm-and-raisepropertychanged/

Oleksandr Kulchytskyi 27-Dec-12 9:21am    
As concerns binding in the code, define a lot of ViewModels and bind it in XAML
ChrisTopherus 27-Dec-12 9:25am    
this referes also to the mvvm model. might be that i have to change a lot... :(
Oleksandr Kulchytskyi 27-Dec-12 9:30am    
Obviously yes, you must(might ) review you architecture approach...
good application design is one of the crucial aspect in software development.

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