Click here to Skip to main content
15,894,017 members

WPF Update Datagrid Binding To ObservableCollection

ChrisTopherus asked:

Open original thread
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...
Tags: C#, .NET, WPF

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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