How can I disable editing cells in a WPF Datagrid?

后端 未结 4 871
你的背包
你的背包 2020-12-24 03:58

I\'m constructing a datagrid in Windows Presentation Foundation, and I have a problem. When a user double-clicks on a cell in my datagrid, the cell goes into edit mode. I wa

相关标签:
4条回答
  • 2020-12-24 04:40

    The WPF DataGrid has an IsReadOnly property that you can set to True to ensure that users cannot edit your DataGrid's cells.

    You can also set this value for individual columns in your DataGrid as needed.

    0 讨论(0)
  • 2020-12-24 04:46

    The DataGrid has an XAML property IsReadOnly that you can set to true:

    <my:DataGrid
        IsReadOnly="True"
    />
    
    0 讨论(0)
  • 2020-12-24 04:54

    If you want to disable editing the entire grid, you can set IsReadOnly to true on the grid. If you want to disable user to add new rows, you set the property CanUserAddRows="False"

    <DataGrid IsReadOnly="True" CanUserAddRows="False" />
    

    Further more you can set IsReadOnly on individual columns to disable editing.

    0 讨论(0)
  • 2020-12-24 04:58

    I see users in comments wondering how to disable cell editing while allowing row deletion : I managed to do this by setting all columns individually to read only, instead of the DataGrid itself.

    <DataGrid IsReadOnly="False">
        <DataGrid.Columns>
            <DataGridTextColumn IsReadOnly="True"/>
            <DataGridTextColumn IsReadOnly="True"/>
        </DataGrid.Columns>
    </DataGrid>
    
    0 讨论(0)
提交回复
热议问题