0

I have a WPF ListView with checkbox in MVVM pattern. I need to accomplish the following two tasks.

1) Sort by any column when I click on column header. If column is already in ascending order then it should reorder in descending and vice versa.

2) SelectedTaskItem is not communicating with ViewModel when i check or uncheck a checkbox.

TaskView.xaml

 <UserControl x:Class="MyProject.TaskView"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              Height="569" Width="954" 
              HorizontalContentAlignment="Center" 
              VerticalContentAlignment="Center" 
   >


    <UserControl.Resources>
       <DataTemplate x:Key="FirstCellCheckBox">
         <CheckBox 
                Command="{Binding IsSelected, Mode= TwoWay}"
                IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource 
                FindAncestor, AncestorType={x:Type ListViewItem}}}"
                CommandParameter="{Binding Path=SelectedTaskItem, 
                ElementName=dgTaskList, UpdateSourceTrigger=PropertyChanged}" 
                />

      </DataTemplate>

    </UserControl.Resources>
    <ListView Grid.Row="1" 
              Name="ListViewTask" 
              Margin="12,49,26,79"  
              ItemsSource="{Binding TaskList}" 
              ScrollViewer.HorizontalScrollBarVisibility="Auto"  
             >

        <ListView.View >
            <GridView x:Name="gvTaskList">
                <GridViewColumn Header="Select" 
                                CellTemplate="{StaticResource FirstCellCheckBox}" 
                                Width="30"/>

                <GridViewColumn Header="Internal File" 
                                DisplayMemberBinding="{Binding TaskID}"  
                                Width="100"/>

                <GridViewColumn Header="TaskDescription" 
                                DisplayMemberBinding="{Binding TaskDescription}"  
                                Width="100" />

                <GridViewColumn Header="Task Status" 
                                DisplayMemberBinding="{Binding TaskStatus}" 
                                Width="100" />
            </GridView>
        </ListView.View>
    </ListView> 

TaskViewModel.cs

 namespace MyProject
 {
  public class TaskViewModel: ViewModelBase
  {
    ObservableCollection<TaskModel> _TaskList;

    public TaskViewModel()
    {
         TaskDAO dal = new TaskDAO();
         _TaskList= dal.GetUpFileList();        
   }


    public ObservableCollection<TaskModel> TaskList 
    {
        get { return _TaskList; }

        set
        {
            if (_TaskList!= value)
            {
                this._TaskList= value;
                this.OnPropertyChanged("TaskList");
            }
        }
    }


    private TaskModel _selectedTaskItem;
    public TaskModel SelectedTaskItem
    {
        get { return _selectedTaskItem; }
        set
        {
            if (value != null)
            {
                _selectedTaskItem= value;
                OnPropertyChanged("SelectedTaskItem");
                if (null != _selectedTaskItem)
                {
                    ObservableCollection<TaskModel> oCol =
                        new ObservableCollection<TaskModel>();
                    foreach (TaskModel itm in TaskList)
                    {
                        if (itm.TaskID == _selectedTaskItem.TaskID)
                        {
                            itm.IsSelected = true;
                        }
                        oCol.Add(itm);

                    }
                    TaskList.Clear();
                    TaskList = oCol;
                    OnPropertyChanged("TaskList");
                }
            }

        }

    } 
  }
Shai
  • 529
  • 7
  • 20
  • 36
  • What is `dgTaskList`? I dont see any element declared with that name. ListView Sorting is explained here ... http://stackoverflow.com/questions/994148/best-way-to-make-wpf-listview-gridview-sort-on-column-header-clicking – WPF-it Oct 20 '11 at 14:39

1 Answers1

2

You are binding the CheckBox's IsChecked value to ListBoxItem.IsChecked, but I don't see anything that binds ListBoxItem.IsChecked to your ViewModel.

Try adding the following to your ListBox.Resources

<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>

For sorting, I'd recommend using a DataGrid instead of a ListView, since sorting is built into the DataGrid. If you don't want to do that, you'll probably have to make some custom ListViewHeaders which execute a SortCommand in your ViewModel

Rachel
  • 130,264
  • 66
  • 304
  • 490
  • 1
    Sorting should be done via an ICollectionView, making the consuming UI type irrelevant. – Aaron McIver Oct 20 '11 at 15:28
  • @AaronMcIver That is true, but you need to have something that tells the ViewModel to sort it's `ICollectionView` when the user clicks on the Header in the ListView, which is why I would suggest overwriting the ListViewHeaders to add that functionality. – Rachel Oct 20 '11 at 15:32