【问题标题】:How to do multiple selections on the datagrid before clicking on datagrid在单击数据网格之前如何在数据网格上进行多项选择
【发布时间】:2012-06-30 00:21:33
【问题描述】:

我有多个选择的 WPF 数据网格(模型具有属性 IsSelected...),它工作正常,但是当我启动程序时,我必须先单击表格,然后才能进行多项选择。当我第一次点击表格时,它会选择光标下的项目(如果我按下了shift,它也会选择项目,但它不会进行多选)。

我想这可能是因为 datagrid 没有聚焦,但是当我在加载的窗口上执行 datagrid.Focus() 时,它也不起作用。我该怎么办?

WPF 数据网格代码:

<DataGrid x:Name="datagrid" AutoGenerateColumns="False" Height="Auto" VerticalAlignment="Stretch" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" Margin="3"
          CanUserAddRows="False" CanUserDeleteRows="False" 
          ItemsSource="{Binding Path=Items}" SelectedItem="{Binding Path=SelectedItem}" IsSynchronizedWithCurrentItem="True"
          SelectedIndex="{Binding Path=SelectedIndex}" SelectionMode="Extended" SelectionUnit="FullRow" EnableColumnVirtualization="False" EnableRowVirtualization="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="{Binding CodebookUnitsUnitTable, Source={StaticResource Texts}}" Binding="{Binding Path=Unit}" Width="*" IsReadOnly="True"/>
        <DataGridTextColumn Header="{Binding CodebookUnitsDescriptionTable, Source={StaticResource Texts}}" Binding="{Binding Path=Description}" Width="5*" IsReadOnly="True"/>
    </DataGrid.Columns>
    <DataGrid.CommandBindings>
        <CommandBinding Command="SelectAll" Executed="CommandBinding_Executed"/>
    </DataGrid.CommandBindings>
    <DataGrid.ContextMenu>
        <ContextMenu>
            <MenuItem Header="{Binding CodebookContextMenuNewItem, Source={StaticResource Texts}}" Command="{Binding NewItemCommand}">
                <MenuItem.Icon>
                    <myComponents:AutoGreyableImage Height="20" Margin="0,-1,0,0" Width="20" Source="../Icons/Small/INovySM.gif" Stretch="None" RenderOptions.BitmapScalingMode="NearestNeighbor" RenderOptions.EdgeMode="Aliased"/>
                </MenuItem.Icon>
            </MenuItem>
        </ContextMenu>
    </DataGrid.ContextMenu>
    <DataGrid.InputBindings>
        <KeyBinding Command="{Binding DeleteCommand}" Key="Delete"/>
    </DataGrid.InputBindings>
</DataGrid>

【问题讨论】:

    标签: wpf datagrid startup multi-select


    【解决方案1】:

    您需要删除与SelectedItem 的绑定,因为这将为整个网格设置一个选定项。

    您可以通过样式绑定到每个数据项的 IsSelected 属性(假设您的视图模型上每个项目都有 IsSelected 属性 - MVVM):

    <DataGrid.Resources>
       <Style TargetType="DataGridRow">
          <Setter Property="IsSelected" Value="{Binding IsSelected}" />
       </Style>
    </DataGrid.Resources>
    

    【讨论】:

    • SelectedItem 没问题。 Items 具有 IsSelected 属性,我在其他资源中有样式。多项选择工作正常,但是当我ShowDialog() 时,我必须先单击表格,然后才能选择多个项目。如果我显示我的对话框,按 shift 并单击表格中的某处,它只选择一项而不是更多选择,执行此操作后,多项选择工作正常。
    【解决方案2】:

    解决方案集中在第一行,而不仅仅是 DataGrid。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-20
      • 2016-03-17
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多