how to sort the items in listbox alphabetically?

Posted by user2745378 on Stack Overflow See other posts from Stack Overflow or by user2745378
Published on 2013-11-04T09:11:57Z Indexed on 2013/11/04 9:54 UTC
Read the original article Hit count: 218

Filed under:
|
|

i need to sort the items alphabetically in listbox when sort button is clicked. (I have sort button in appbar). But I dunno how to achieve this. here is XAML. All help will be much appreciated.

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="ProjectTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="400" />
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="1" Text="{Binding Name}" Style="{StaticResource PhoneTextLargeStyle}" />
        </Grid>
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,0,12,0">
    <ListBox x:Name="projectList" ItemsSource="{Binding Items}" SelectionChanged="ListBox_SelectionChanged" ItemTemplate="{StaticResource ProjectTemplate}" />
</Grid>

Here's my ViewModel

namespace PhoneApp.ViewModels
{
public class ProjectsViewModel: ItemsViewModelBase<Project>
{
    public ProjectsViewModel(TaskDataContext taskDB)
        : base(taskDB)
    {
    }

    public override void LoadData()
    {
        base.LoadData();

        var projectsInDB = _taskDB.Projects.ToList();

        Items = new ObservableCollection<Project>(projectsInDB);
    }

    public override void AddItem(Project item)
    {
        _taskDB.Projects.InsertOnSubmit(item);
        _taskDB.SubmitChanges();

        Items.Add(item); 
    }

    public override void RemoveItem(int id)
    {
        var projects = from p in Items
                  where p.Id == id
                  select p;

        var item = projects.FirstOrDefault();


        if (item != null)
        {
            var tasks = (from t in App.TasksViewModel.Items
                         where t.ProjectId == item.Id
                         select t).ToList();

            foreach (var task in tasks)
                App.TasksViewModel.RemoveItem(task.Id);

            Items.Remove(item);

            _taskDB.Projects.DeleteOnSubmit(item);
            _taskDB.SubmitChanges();
        }
    }       
}

}

I have added the ViewModel C# Code herewith

© Stack Overflow or respective owner

Related posts about c#

Related posts about windows-phone-8