Click here to Skip to main content
14,389,295 members
Rate this:
Please Sign up or sign in to vote.
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:

public partial class AddressPage : Page
        ObservableCollection<Address> AddressCollection =
        new ObservableCollection<Address>();
        public AddressPage()

        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)
            new Address { address1 = "test1", address2 = "test2" });

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

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

<DataGrid x:Name="MainGrid" ItemsSource="{Binding AddressCollection}"  AutoGenerateColumns="False">
        <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...

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1


" 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).
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:
public class MainViewModel
private ObservableCollection<address> _addressCollection;
public ObservableCollection<address> AddressCollection
  get{ return _addressCollection;}
  set{ _addressCollection=value; RaiseProperchyChanged("AddressCollection")}

<datagrid x:name="MainGrid" itemssource="{Binding AddressCollection}" datacontext="{Binding" mode="hold" xmlns:x="#unknown" />
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

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.
ChrisTopherus 27-Dec-12 9:36am
thank you for your help. PS: you can have a clean design without patterns like these ;)
Oleksandr Kulchytskyi 27-Dec-12 9:43am
Agree , patterns is not a bible, but they are something that devs you should consider :)
ChrisTopherus 27-Dec-12 10:14am
I'm reading this great article at the moment and i think i will use this pattern.
But do you know, that there is no other way to bind via xaml for me? (so i can mark this question as answered...)
Oleksandr Kulchytskyi 27-Dec-12 10:22am
"But do you know, that there is no other way to bind via xaml for me? (so i can mark this question as answered...)" - what do you mean?
There are 2 ways to declare bindings: imperative and Declarative.
The way like you did is called imperative, the way like i suggest is declarative.
ChrisTopherus 27-Dec-12 10:34am
And the declarative way is only possible over the mvvm pattern?
Oleksandr Kulchytskyi 27-Dec-12 11:13am
=) Well, obviously, you didnt catch the whole idea of MVVM pattern, the main idea is delimnit your logic from view , though , there must be no code in your view....

So lets, go back to your question, it's possible, buts from code clean consideration more polite will be usage of declarative
ChrisTopherus 27-Dec-12 11:27am
Okay... as more as i read about mvvm as better it looks to me. my project will be wrapped in this pattern an then i will use the way you described. thank you!
Oleksandr Kulchytskyi 27-Dec-12 11:32am
=)I wish you good luck !) Have a nice coding procedures ;)

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100