0

I have a listview in my WPF application and the first column is a Checkbox. This checkbox is bound to the IsSelected property of my model and the event propogation happens correctly.

I also have a Checkbox in the same column's header and want to implement a 'Select All' feature where it checks all the listview items.

I'm using pattern MVVM.

The Event doesn't fire!

Can someone explain what I am doing wrong here..

The relevant code portions are mentioned below..

XAML:

<ListView Grid.Row="0"
                            ItemsSource="{Binding Path=WorkOrders}"
                            Margin="5,10,5,5" 
                            Name="WorkOrders" 
                            SelectionMode="Multiple"
                            FontSize="13"
                            Background="AliceBlue"
                            BorderBrush="AliceBlue">

            <!--Style of items-->
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <!--Properties-->
                    <Setter Property="Control.HorizontalContentAlignment" Value="Stretch" />
                    <Setter Property="Control.VerticalContentAlignment" Value="Center" />
                    <!--Trigger-->
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="{x:Null}" />
                            <Setter Property="BorderBrush" Value="{x:Null}" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>

            <ListView.View>
                <GridView >
                    <GridViewColumn CellTemplate="{StaticResource CheckBoxDataTemplate}" Width="80" >
                        <GridViewColumn.HeaderTemplate>
                            <DataTemplate>

                                    <CheckBox Command="{Binding Path=SelectAllCommand}"  />
                            </DataTemplate>
                        </GridViewColumn.HeaderTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="WorkOrder" CellTemplate="{StaticResource DetailIdenTemplate}"  Width="300"/>
                </GridView>
            </ListView.View>
        </ListView>

Model:

public class WorkOrder
{
    public int CD_WORK_ORDER { get; set; }
    public string ID_WORK_ORDER { get; set; }
    public bool IsSelected { get; set; }
}

ViewModel:

    public class LockWorkOrderSelectionViewModel : ViewModelBase
        {
            RelayCommand _selectAllCommand;
            public ICommand SelectAllCommand
            {
                get
                {
                    if (_selectAllCommand == null)
                    {
                        _selectAllCommand = new RelayCommand(
                            param => SelectAllElement(),
                            param => CanSelectAll);
                    }
                    //RaiseEvent(new RoutedEventArgs(SearchEvent));
                    return _selectAllCommand;
                }
            }

            private bool _selectedAllElement;
            public bool SelectAllElement()
            {
                foreach (var item in WorkOrders)
                {
                    item.IsSelected = true;
                }
                return true;
            }

public bool CanSelectAll
        {
            get { return true; }
        }

        public List<string> WorkOrdersList
        {
            get { return _workOrdersList; }
        }

        private ObservableCollection<WorkOrder> _workOrders = new ObservableCollection<WorkOrder>();
        public ObservableCollection<WorkOrder> WorkOrders
        {
            get
            {
                int progr = 1;
                foreach (var item in WorkOrdersList)
                {
                    if (_workOrders.FirstOrDefault(i => i.ID_WORK_ORDER == item) == null)
                    {
                        _workOrders.Add(new WorkOrder { CD_WORK_ORDER = progr, ID_WORK_ORDER = item, IsSelected = false });
                        progr++;
                    }
                }
                return _workOrders;
            }
        }
    }
1
  • Have you tried giving command a relative Source to view model which is control.dataContext ? Commented Feb 16, 2015 at 17:06

1 Answer 1

1
<CheckBox IsChecked="{Binding DataContext.SelectAll, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />

Works for me.

Sign up to request clarification or add additional context in comments.

3 Comments

this works on IsChecked, and should work on command as well, let me know once u change?
This should work for a command.. '<CheckBox CommandParameter="{Binding}" Command="{Binding DataContext.SelectAllCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">'
I would leave the answer as for UserControl because in most of the cases we deal with userControls and not windows. Thanks for the suggestion anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.