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;
}
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...