xsheru
xsheru

Reputation: 497

Sort ListBox (Windows Phone)

How to sort items automatically in ListBox (Windows Phone App)?

My List Box is 'AccountsList' & I want to sort it by the column 'AccountName' Please Help... I cant find any ListBox.Sorted Property in Windows Phone. I tried couple of thing which are found in StackOverflow but didnt work PLEASE HELP.

View Page

public partial class Accounts : PhoneApplicationPage
{
    private const string strConnectionString = @"isostore:/xCryptoDB.sdf";
    public string a;

    public Accounts()
    {
        InitializeComponent();

        using (xCryptoDataContext context = new xCryptoDataContext(strConnectionString))
        {
            if (!context.DatabaseExists())
            {
                context.CreateDatabase();
            }
        }


        using (xCryptoDataContext xCryptoDB = new xCryptoDataContext(strConnectionString))
        {
            var a = from b in xCryptoDB.GetTable<AccountsTable>() select b.Extra;
            List<AccountsTable> dataSource = new List<AccountsTable>();
            foreach (var x in a)
            {
                dataSource.Add(new AccountsTable() { Extra = x });
            }
            this.AccountsList.ItemsSource = dataSource;

            if (AccountsList.Items.Count == 0)
            {
                AccountsList.Visibility = Visibility.Collapsed;
            }
            else
            {
                AccountsList.Visibility = Visibility.Visible;
            }
        }
    }

    private void AccountsList_DoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        var listBoxItem = AccountsList.ItemContainerGenerator.ContainerFromIndex(AccountsList.SelectedIndex) as ListBoxItem;
        var txtBlk = FindVisualChildByType<TextBlock>(listBoxItem, "txtBlkExtra");
        a = txtBlk.Text;
        NavigationService.Navigate(new Uri(string.Format("/ViewAccount.xaml?parameter={0}&action={1}", a.ToString(), "View"), UriKind.Relative));
    }

    T FindVisualChildByType<T>(DependencyObject element, String name) where T : class
    {
        if (element is T && (element as FrameworkElement).Name == name)
            return element as T;
        int childcount = VisualTreeHelper.GetChildrenCount(element);
        for (int i = 0; i < childcount; i++)
        {
            T childElement = FindVisualChildByType<T>(VisualTreeHelper.GetChild(element, i), name);
            if (childElement != null)
                return childElement;
        }
        return null;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        NavigationService.Navigate(new Uri(string.Format("/AddAccount.xaml?parameter={0}&action={1}", "parameterValue", "Add"), UriKind.Relative));
    }

Add Account Page

public partial class AddAccount : PhoneApplicationPage
{
    private const string strConnectionString = @"isostore:/xCryptoDB.sdf";
    public string parameterValue;
    public string Action;
    public string id;
    public AddAccount()
    {
        InitializeComponent();
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {

        base.OnNavigatedTo(e);
        parameterValue = NavigationContext.QueryString["parameter"];
        Action = NavigationContext.QueryString["action"];

        using (xCryptoDataContext xCryptoDB = new xCryptoDataContext(strConnectionString))
        {
            var a = from b in xCryptoDB.GetTable<AccountsTable>() where b.Extra == parameterValue.ToString() select b;
            foreach (var x in a)
            {
                txtAccountName.Text = x.AccountName;
                txtWebAdd.Text = x.WebAdd;
                txtEmailID.Text = x.EmailID;
                txtUserID.Text = x.UserID;
                txtPassword.Text = x.Password;
                txtNote.Text = x.Note;
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        using (xCryptoDataContext xCryptoDB = new xCryptoDataContext(strConnectionString))
        {
            AccountsTable newACTable = new AccountsTable
            {
                Extra = txtAccountName.Text.ToString(),
                AccountName = txtAccountName.Text.ToString(),
                WebAdd = txtWebAdd.Text.ToString(),
                EmailID = txtEmailID.Text.ToString(),
                UserID = txtUserID.Text.ToString(),
                Password = txtPassword.Text.ToString(),
                Note = txtNote.Text.ToString()
            };
            var a = from b in xCryptoDB.GetTable<AccountsTable>() select b;

            if (txtAccountName.Text != "")
            {
                xCryptoDB.ACTable.InsertOnSubmit(newACTable);
                xCryptoDB.SubmitChanges();
                MessageBox.Show("Account is Added To Database.");
            }
            txtAccountName.Text = "";
            txtWebAdd.Text = "";
            txtEmailID.Text = "";
            txtUserID.Text = "";
            txtPassword.Text = "";
            txtNote.Text = "";
        }
    }
}

Upvotes: 0

Views: 1244

Answers (2)

FunksMaName
FunksMaName

Reputation: 2111

Any reason why you don't want to order the list after retrieval?

//Ascending
var a = (from b in xCryptoDB.GetTable<AccountsTable>() select b.Extra).OrderBy(e => e)

//Descending
var a = (from b in xCryptoDB.GetTable<AccountsTable>() select b.Extra).OrderByDescending(e => e)

Upvotes: 1

konyaku
konyaku

Reputation: 36

could you please to give a screenshot of the application or the code..

Do it in .cs not in .xaml

or try this one : http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.sort.aspx

Upvotes: 0

Related Questions