WPF ListView Header 复选框和 MVVM 命令

WPF ListView Header Checkbox and MVVM Command

我的 WPF 应用程序中有一个列表视图,第一列是复选框。此复选框绑定到我的模型的 IsSelected 属性,并且事件传播正确发生。

我在同一列的 header 中也有一个复选框,我想实现一个 'Select All' 功能来检查所有列表视图项目。

我正在使用模式 MVVM。

事件没有触发!

谁能解释一下我做错了什么..

相关代码部分如下..

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>

型号:

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

视图模型:

    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;
            }
        }
    }
<CheckBox IsChecked="{Binding DataContext.SelectAll, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />

适合我。