Search code examples
c#wpfmvvmdatacontext

wpf UI not updating on data context change


I am developing and wpf app and in which I need to update data on basis of click on button. I tried to update in code behind but it did not work so I used datacontext but still no use. I saw various solutions and have used mode=TwoWay, UpdateSourceTrigger but it does not work.

<Grid DataContext="{Binding Dashboard, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
//Content
</Grid>

In cs file

public ViewModels.DashboardVM _DashVM = new ViewModels.DashboardVM();

async private void DashboardPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                await _DashVM.GetDashboardData();
                this.DataContext = _DashVM;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write(ex.Message);
            }
        }

and changing data context here

async private void StoresList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var item = (sender as ListView).SelectedItem as Models.StoresLM;
                if(item!=null)
                {
                    Properties.Settings.Default.StoreId = item.id;
                    Properties.Settings.Default.Save();
                    await _DashVM.GetDashboardData();

                    this.DataContext = _DashVM;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write(ex.Message);
            }
        }

my View Model is

public class DashboardVM : INotifyPropertyChanged
{
    private Models.DashboardM _dashboard;
    public Models.DashboardM Dashboard
    {
        get { return _dashboard; }
        set { _dashboard = value; RaisePropertyChanged("Dashboard"); }
    }

    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public static event EventHandler<Boolean> IsLoading = delegate { };

    async public Task<Boolean> GetDashboardData()
    {
        try
        {
            if (InternetTools.IsNetworkConnected())
            {
                IsLoading(this, true);
                var storeId = Convert.ToString(Properties.Settings.Default.StoreId);
                var Response = await new WebServiceUtility().PostRequest(string.Format(StringUtility.DashboardApi, Convert.ToString(Properties.Settings.Default.StoreId)), new[] { new KeyValuePair<string, string>("api_key", "dbjh") });
                if (Response.IsSuccessStatusCode)
                {
                    var DashboardData = await Response.Content.ReadAsStringAsync();
                    var jsonObject = Newtonsoft.Json.Linq.JObject.Parse(DashboardData);
                    if (Convert.ToString(jsonObject["success"]).IndexOf("True", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        var DashObject = jsonObject.ToObject<Models.DashboardM>();
                        Properties.Settings.Default.Currency = DashObject.data.store.currency.StartsWith("&") ? System.Net.WebUtility.HtmlDecode(DashObject.data.store.currency) : System.Text.RegularExpressions.Regex.Unescape(DashObject.data.store.currency);
                        DashObject.data.store.currency = StringUtility.Currency;
                        Properties.Settings.Default.Save();
                        Dashboard = null;
                        Dashboard = DashObject;
                    }
                }
            }
            else
                NotificationUtility.ShowErrorMessage(NotificationUtility.MsgType.InternetError);
        }
        catch
        {

        }
        IsLoading(this, false);
        return true;
    }
    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}

Can anybody help?


Solution

  • I was able to resolve the problem by editing the code. It was not getting notified due to reassigning of object. I don't know the reason but I just changed this code

    async private void StoresList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var item = (sender as ListView).SelectedItem as Models.StoresLM;
                if(item!=null)
                {
                    Properties.Settings.Default.StoreId = item.id;
                    Properties.Settings.Default.Save();
                    await _DashVM.GetDashboardData();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write(ex.Message);
            }
        }
    

    Hope it helps somebody like me.