0

如何在 ListBox(Windows Phone 应用程序)中自动对项目进行排序?

我的列表框是“AccountsList”,我想按“AccountName”列对其进行排序,请帮助...我在 Windows Phone 中找不到任何 ListBox.Sorted 属性。我尝试了一些在 StackOverflow 中找到的东西,但没有用,请帮助。

查看页面

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));
    }

添加帐户页面

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 = "";
        }
    }
}
4

2 回答 2

1

为什么您不想在检索后订购列表?

//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)
于 2013-09-04T08:32:08.720 回答
0

能否请您提供应用程序或代码的屏幕截图..

在 .cs 中而不是在 .xaml 中执行

或试试这个:http: //msdn.microsoft.com/en-us/library/system.windows.forms.listbox.sort.aspx

于 2013-09-04T07:02:32.100 回答