Hi i have observablecollection and i takes data from server and when it finish i want refresh list on my page.Data is coming but i cant see it on screen i cant find my error. I implement INotifypropertychanged bu i think its not working well. When i put breakpoint i see PropertyChanged event every time is null.
My class
public class Ulke:INotifyPropertyChanged
{
private int _ulkeID;
public int UlkeID
{
get { return _ulkeID; }
set { _ulkeID = value; NotifyPropertyChanged(); }
}
private string _adi;
public string Adi
{
get { return _adi; }
set { _adi = value; NotifyPropertyChanged(); }
}
public string DiyanetID { get; set; }
public bool EyaletVarmi { get; set; }
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
UlkeRepository Class
public class UlkeRepository : INotifyPropertyChanged
{
public UlkeRepository()
{
_ulkeler = new ObservableCollection<Ulke>();
ulkeler.Add(new Ulke { UlkeID = 3, EyaletVarmi = true, DiyanetID = "3", Adi = "Moliba" });
ulkeler.Add(new Ulke { UlkeID = 5, EyaletVarmi = true, DiyanetID = "3", Adi = "As" });
}
private ObservableCollection<Ulke> _ulkeler;
public ObservableCollection<Ulke> ulkeler
{
get { return _ulkeler; }
set { _ulkeler = value; NotifyPropertyChanged(); }
}
public async Task UlkeGetir()
{
JsonDownloader js = new JsonDownloader();
var result = await js.GetDataFromUrl<Ulke>("http://www.example.com/A/UlkeGetir");
ulkeler = result;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
MyPageLoaded code
this.DataContext = ulke.ulkeler;
liste.DataContext = ulke.ulkeler;
ulke.UlkeGetir();
Xaml Code
<ListBox x:Name="liste"
ItemsSource="{Binding Mode=TwoWay}" SelectionChanged="liste_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Adi}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Updated Solution after taking a look at the source:
Everything is working, you just need to pay attention to the compiler warnings.
HubPage.xaml.cs
Function UlkeYukle Update (you forgot to await)
UlkeRepository ulke;
private async void UlkeYukle()
{
ulke = new UlkeRepository();
await ulke.UlkeGetir(); // you forgot this
}
// this is a function to test your OnPropertyChange
private void Button_Click(object sender, RoutedEventArgs e)
{
ulke.ulkeler.Insert(0, new Ulke { UlkeID = 5, EyaletVarmi = true, DiyanetID = "3", Adi = "A_TEST" }); // inssert at top
ulke.ulkeler[0].Adi = "Changing Title Again";
}
For debug purpose I added a Button to the template (so we can test if it updates after the fact)
HubPage.xaml
<HubSection x:Uid="HubSection1" x:Name="hub1" Header="SECTION 1" DataContext="{Binding Mode=TwoWay}" HeaderTemplate="{ThemeResource HubSectionHeaderTemplate}">
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80*"/>
<RowDefinition Height="20*"/>
</Grid.RowDefinitions>
<ListView
ItemsSource="{Binding Mode=TwoWay}"
IsItemClickEnabled="True"
ItemClick="GroupSection_ItemClick"
ContinuumNavigationTransitionInfo.ExitElementContainer="True">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,27.5">
<TextBlock Text="{Binding Adi}" Style="{ThemeResource ListViewItemTextBlockStyle}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Click="Button_Click" Grid.Row="1"></Button>
</Grid>
</DataTemplate>
</HubSection>