Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET WPF
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()
        {
            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...
 
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Style="{StaticResource standardDataPage}"
d:DesignHeight="300" d:DesignWidth="300"
Title="AddressPage">
<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 27-Dec-12 3:40am

1 solution

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

Solution 1

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:
public class MainViewModel
{
private ObservableCollection<address> _addressCollection;
public ObservableCollection<address> AddressCollection
{
  get{ return _addressCollection;}
  set{ _addressCollection=value; RaiseProperchyChanged("AddressCollection")}
} 
}
</address></address>
<datagrid x:name="MainGrid" itemssource="{Binding AddressCollection}" datacontext="{Binding" mode="hold" xmlns:x="#unknown" />
  Permalink  
Comments
ChrisTopherus at 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 at 27-Dec-12 9:20am
   
As concerns RaisePropertyChanged read this http://wilberbeast.com/2010/07/21/wpf-mvvm-and-raisepropertychanged/

Oleksandr Kulchytskyi at 27-Dec-12 9:21am
   
As concerns binding in the code, define a lot of ViewModels and bind it in XAML
ChrisTopherus at 27-Dec-12 9:25am
   
this referes also to the mvvm model. might be that i have to change a lot... :(
Oleksandr Kulchytskyi at 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 at 27-Dec-12 9:36am
   
thank you for your help. PS: you can have a clean design without patterns like these ;)
Oleksandr Kulchytskyi at 27-Dec-12 9:43am
   
Agree , patterns is not a bible, but they are something that devs you should consider :)
ChrisTopherus at 27-Dec-12 10:14am
   
I'm reading this great article at the moment http://www.codeproject.com/Articles/100175/Model-View-ViewModel-MVVM-Explained 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 at 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 at 27-Dec-12 10:34am
   
And the declarative way is only possible over the mvvm pattern?
Oleksandr Kulchytskyi at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 490
1 Maciej Los 299
2 BillWoodruff 174
3 /\jmot 170
4 Suraj Sahoo | Coding Passion 150
0 OriginalGriff 8,484
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,159
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 27 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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