WPF DataGrid多选功能开发

<DataTemplate x:Key="CheckBoxDataTemplate">
            <Grid x:Name="Grid"  HorizontalAlignment="Center" VerticalAlignment="Center">
                <CheckBox
                    Click="CheckBox_Checked"
                    Uid="{Binding Name}">

                </CheckBox>
            </Grid>
            <DataTemplate.Triggers>
                <Trigger Property="Selector.IsSelected" Value="True">
                    <Setter  TargetName="Grid" Property="Background" Value="White"></Setter>
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>

        <Style x:Key="CheckBoxStyle" TargetType="{x:Type DataGridCell}">
            <Setter Property="ContentTemplate" Value="{StaticResource  CheckBoxDataTemplate}"/>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Foreground" Value="#606266" />
                    <Setter Property="Background" Value="White" />
                    <Setter Property="BorderBrush" Value="Transparent"></Setter>
                </Trigger>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#f5f7fa" />
                    <!--<Setter Property="Foreground" Value="White"/>-->
                </Trigger>
            </Style.Triggers>
        </Style>

grid checkbox列设置

 <DataGrid  Name="ListGrid" SelectionMode="Extended" VirtualizingStackPanel.IsVirtualizing="False" ItemsSource="{Binding LaryerList}"   Margin="0" AutoGenerateColumns="False" IsReadOnly="True" SelectedIndex="-1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent"  CanUserSortColumns="False" CanUserResizeRows="False" CanUserResizeColumns="False" CanUserReorderColumns="False" DataGridRow.MouseUp="ListGrid_OnMouseUp">
                    <DataGrid.Columns>
                        <DataGridCheckBoxColumn Header="选择" CellStyle="{StaticResource  CheckBoxStyle}">
                            <DataGridCheckBoxColumn.HeaderTemplate>
                                <DataTemplate>
                                    <CheckBox   x:Name="ckbSelectedAll" Uid="-1" Content="全选" Click="CheckBox_Checked" IsChecked="False" />
                                </DataTemplate>
                            </DataGridCheckBoxColumn.HeaderTemplate>
                            
                        </DataGridCheckBoxColumn>
                        <DataGridTextColumn Binding="{Binding itemName}" Header=""  />
                    </DataGrid.Columns>
                </DataGrid>

后台代码实现:

   private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox checkBox = (CheckBox)sender;
            var viewModel = (SelectPositionPrintViewModel)this.DataContext;
            if (checkBox.Uid == "-1")
            {
                if (checkBox.IsChecked == true)
                {
                    AllCheckBoxCheck(true);
                    this.ListGrid.SelectAll();

                    viewModel.SelectItemList = ListGrid.SelectedItems.Cast<ItemTreeData>().ToList();
                }
                else
                {

                    AllCheckBoxCheck(false);
                    this.ListGrid.UnselectAll();
                    viewModel.SelectItemList = ListGrid.SelectedItems.Cast<ItemTreeData>().ToList();
                }
            }
            else
            {
                var currentItem = (ItemTreeData)this.ListGrid.SelectedItem;

if (checkBox.IsChecked == true)
{
if (!viewModel.SelectItemList.ToList().Exists(c => c.itemName == checkBox.Uid))
{
viewModel.SelectItemList.Add(viewModel.LaryerList.FirstOrDefault(c => c.itemName == checkBox.Uid));
}
}
else
{
viewModel.SelectItemList.RemoveAll(c => c.itemName == checkBox.Uid);


}


            }
 private void AllCheckBoxCheck(bool isChecked)
        {
            foreach (var item in ListGrid.Items)
            {
                DataGridRow dr = (DataGridRow)ListGrid.ItemContainerGenerator.ContainerFromItem(item);
                VisualHelper.GetVisualChildCheckBox(dr, isChecked);
            }
        }
 public class VisualHelper
    {
        /// <summary>
        /// 获取checkBox并设置选中
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="isChecked"></param>
        public static void GetVisualChildCheckBox(DependencyObject parent, bool isChecked)
        {
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                DependencyObject v = (DependencyObject)VisualTreeHelper.GetChild(parent, i);
                CheckBox child = v as  CheckBox;

                if (child == null)
                {
                    GetVisualChildCheckBox(v, isChecked);
                }
                else
                {
                    child.IsChecked = isChecked;
                    return;
                }
            }
        }
    }

 

posted @ 2020-05-27 13:16  _York  阅读(165)  评论(0编辑  收藏