wpf datagrid绑定行选中状态

样式如下

      <DataGrid Margin="0,6,0,0" HeadersVisibility="All" RowHeaderWidth="60" HorizontalScrollBarVisibility="Visible" AutoGenerateColumns="False"  
                  ItemsSource="{Binding DisplayConfigList}" hc:DataGridAttach.ShowRowNumber="False" hc:DataGridAttach.ShowSelectAllButton="True" 
                  >
            <DataGrid.RowHeaderTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox IsChecked="{Binding IsSelected,RelativeSource={RelativeSource AncestorType=DataGridRow}}" Name="dataGridRowCheckBox">
                            <hc:Interaction.Triggers>
                                <hc:EventTrigger EventName="Checked">
                                    <!--<hc:EventToCommand Command="{Binding  CheckCommand}" PassEventArgsToCommand="True" />-->
                                    <hc:EventToCommand Command="{Binding DataContext.CheckCommand,RelativeSource={RelativeSource AncestorType=DataGrid}}" CommandParameter="{Binding .,RelativeSource={RelativeSource AncestorType=DataGridRow}}" />
                                </hc:EventTrigger>
                                <hc:EventTrigger EventName="Unchecked">
                                    <!--<hc:EventToCommand Command="{Binding  CheckCommand}" PassEventArgsToCommand="True" />-->
                                    <hc:EventToCommand Command="{Binding DataContext.CheckCommand,RelativeSource={RelativeSource AncestorType=DataGrid}}" CommandParameter="{Binding .,RelativeSource={RelativeSource AncestorType=DataGridRow}}" />
                                </hc:EventTrigger>
                            </hc:Interaction.Triggers>
                        </CheckBox>
                    </StackPanel>
                </DataTemplate>
            </DataGrid.RowHeaderTemplate>
            <DataGrid.Columns>
                <!--<DataGridCheckBoxColumn  Width="auto"  Binding="{Binding ISSelected,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="任务状态"/>-->
                <DataGridTemplateColumn Header="操作">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                                <TextBlock Text="{Binding ISSelected}"/>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

command内容如下

  CheckCommand = new BaseCommand((para) =>
  {
      if(para is DataGridRow row)
      {
          if(row.DataContext!=null && row.DataContext is VMIndexRunningConfig config)
          {
              config.ISSelected = row.IsSelected;
          }
      }
  });
posted @ 2024-06-06 15:15  Hey,Coder!  阅读(9)  评论(0编辑  收藏  举报