Question

Ok, let's see... I have this ViewModel Class, it has a builder to retrieve data from storage, another method who asks data to Internet, it has a saver the notifypropertychange methods and the locator methods. But the problem is how can I Bind it to a jumplistbox, since as I'm using Locator Binding I can't process it as a group to take the headers and order it.

public class FriendLocator
{
    private Lazy<ViewModelFriends> mainVMFriends;

    public FriendLocator()
    {
        mainVMFriends = new Lazy<ViewModelFriends>(() => new ViewModelFriends());
    }

    public ViewModelFriends MainVMFriends
    {
        get
        {
            return mainVMFriends.Value;
        }
    }
}

public class VMFriendsBase : INotifyPropertyChanged
{
    public VMFriendsBase()
    { }

    public event PropertyChangedEventHandler PropertyChanged;

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

public class ViewModelFriends : VMFriendsBase
{
    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
    private ObservableCollection<User_friend> Friends;

    public ObservableCollection<User_friend> friends
    {
        get
        {
            return Friends;
        }
        set
        {
            Friends = value;
            RaisePropertyChanged("friends");
        }
    }

    public ViewModelFriends()
    {
        ObservableCollection<User_friend> a = new ObservableCollection<User_friend>();
        if (settings.Contains("userFriends"))
        {
            string listado = settings["userFriends"] as string;
            var listajson = JsonConvert.DeserializeObject<objetoslistas.getmyfriends>(listado);
            if (listajson.profile != null)
            {
                foreach (objetoslistas.User user in listajson.profile)
                {
                    User_friend usuario = new User_friend(user);
                    a.Add(usuario);
                }
            }
        }
        friends = a;
        AskFriends();
    }
    public async void AskFriends()
    {
        ObservableCollection<User_friend> a = new ObservableCollection<User_friend>();
        Uri url = new Uri(link);
        objetoslistas.GetByUserID paquete = new objetoslistas.GetByUserID();
        string respuesta = await metodosJson.jsonPOST(url, paquete);
        var respuestajson = JsonConvert.DeserializeObject<objetoslistas.getmyfriends>(respuesta.ToString());
        if (respuestajson.error == "")
        {
            saveFriends(respuesta);
            if (respuestajson.profile != null)
            {
                foreach (objetoslistas.User user in respuestajson.profile)
                {
                    User_friend usuario = new User_friend(user);
                    a.Add(usuario);
                }
            }
        }
        friends = a;
    }
    public void saveFriends(string jsonfriends)
    {
        if (!settings.Contains("userFriends"))
            settings.Add("userFriends", jsonfriends);
        else
            settings["userFriends"] = jsonfriends;
        settings.Save();
    }
}

The methods I use to process data to listbox are

public static List<Group<T>> GetItemGroups<T>(IEnumerable<T> itemList, Func<T, string> getKeyFunc)
{
    IEnumerable<Group<T>> groupList = from item in itemList
        group item by getKeyFunc(item) into g
        orderby g.Key
        select new Group<T>(g.Key, g);

    return groupList.ToList();
}

public class Group<T> : List<T>
{
    public Group(string name, IEnumerable<T> items)
        : base(items)
    {
        this.Title = name;
    }

    public string Title
    {
        get;
        set;
    }
}

Thanks to all

Was it helpful?

Solution

Ok, I finaly solved by myself, I made a conversor like this:

public class ObservableToGroupedConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        var group = (value as ViewModelFriends).friends;
        return metodosAuxiliares.GetItemGroups(group.OrderBy(o => o.distraw).ToList(), c => c.online);
    }
    public object ConvertBack(object value, Type targetType,
       object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

I added the conversor to the resources and the binding is:

<view:ViewFriends x:Name="vistaAmigos" 
    DataContext="{Binding MainVMFriends,
    Source={StaticResource friendLocator},
    Converter={StaticResource friendConvert}}"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top