I am currently trying to make a client-side chat app with C#, so ı am using WPF for UI. But there is a problem, when ı try to bind data to source. view can receive data from viewmodel but just once. I have tried much ways, finally ı think ı have problem in OnPropertyChanged event. But I can't find the trouble.
Here are my codes:
class ObservableObject: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChaned([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Data from MainViewModel (I implemented this class from ObservableObject Class)
private string _contactName;
public string ContactName { get { return _contactName; } set { _contactName = value; OnPropertyChanged(); } }
finally, the xaml code (At the beginning of the xaml code, ı imported the mainviewmodel as datacontext)
<TextBlock>
<!--name-->
<Run
Text="{Binding ContactName, UpdateSourceTrigger=PropertyChanged
,FallbackValue='Kişi İsmi', TargetNullValue='Kişi İsmi'}"
FontWeight="Bold"
FontSize="20"/>
<LineBreak/>
<!--online status-->
<Run
Text="{Binding LastSeen, UpdateSourceTrigger= PropertyChanged}"
FontSize="15"/>
</TextBlock>
In the above, just an example ı have same problem all of the datas.
When I changed the ContactName from MainViewModel with buttonclick event e.t.c there is nothing happens.
I Found the problem,
while ı'm creating the ObservableObject Class, I did not implement it from INotifyPropetyChanged interface. Then the updated ObservableObject Class must be like that:
class ObservableObject: INotifyPropertyChanged
{
//Bu sınıf MVVM kalıbımızın ViewModel Katmanının View katmanı tarafından anlaşılabilmesi için, built-in olarak verilen INotifyPropertyChanged interface'ini daha kolay hale getirmiştir.
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChaned([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}