简体   繁体   English

WPF DataGrid - 如何在按钮单击时使选定的行可编辑

[英]WPF DataGrid - How to make selected row editable on Button click

The following DataGrid is set to ReadOnly .以下 DataGrid 设置为ReadOnly But, when Edit Button on a row is clicked I need that row to become editable.但是,当单击一行上的Edit Button时,我需要该行变得可编辑。 How can we achieve this task?我们如何才能完成这项任务?

Please note that the Edit button click event is still fired on a ReadOnly grid.请注意,在 ReadOnly 网格上仍会触发 Edit 按钮单击事件。 Maybe, in this event we need to set DataGridCell.IsEditing Property to true for each cell on that row.也许,在这种情况下,我们需要为该行上的每个单元格设置DataGridCell.IsEditing属性为 true。 But I am not able to get the DataGridCell object in the row.但我无法在行中获得DataGridCell object Or, there may be some better options.或者,可能有一些更好的选择。

<Window x:Class="MyTestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        .....
        Title="MainWindow">
    <Grid>
        <DataGrid x:Name="MyDatagrid" IsReadOnly="True" AutoGenerateColumns="False" SelectionMode="Single">
            <DataGrid.Columns>
               <DataGridTemplateColumn Header="Edit">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button x:Name="btnEdit" Content="Edit" Click="btnEdit_Click"></Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
               </DataGridTemplateColumn>
                <DataGridTextColumn Header="ID" Visibility="Collapsed" Binding="{Binding MyClassId}" />
                <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
                <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

MyClass and other relevant code : MyClass 和其他相关代码

public class MyClass
{
    [Key]
    public int MyClassId { get; set; }
    public string FirstName{ get; set; }
    public string LastName{ get; set; }
    .........
    .........
}

//DataGrid binding to source:
List<MyClass> lstMyClass = db.MyClass.ToList();
MyDatagrid.ItemsSource = lstMyClass;
............
.........
 <DataGrid x:Name="MyDatagrid" AutoGenerateColumns="False" SelectionMode="Single">
        <DataGrid.Columns>
           <DataGridTemplateColumn Header="Edit">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ToggleButton x:Name="btnEdit" Content="Edit" IsChecked="{Binding Is_Checked}"></ToggleButton>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
           </DataGridTemplateColumn>
            <DataGridTextColumn Header="ID" Visibility="Collapsed" Binding="{Binding MyClassId}" />
            <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" IsReadOnly="{Binding Is_Checked}"/>
            <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" IsReadOnly="{Binding Is_Checked}"/>
        </DataGrid.Columns>
    </DataGrid>

First, Each Column Must Have IsReadOnly.首先,每列必须有 IsReadOnly。

And IsReadOnly Binding MyClass's Is_Checked.并且 IsReadOnly 绑定 MyClass 的 Is_Checked。

ToggleButton's IsChecked Binding MyClass's Is_Checked,too. ToggleButton 的 IsChecked 也绑定了 MyClass 的 Is_Checked。

Then, When ToggleButton Click, Change Column's IsReadOnly.然后,当 ToggleButton 单击时,更改列的 IsReadOnly。

All of this should be MVVM所有这些都应该是 MVVM

public class MyClass
{
    [Key]
    public int MyClassId { get; set; }
    public string FirstName{ get; set; }
    public string LastName{ get; set; }
    public bool Is_Checked{ get; set; }
    .........
    .........
}

Set the CurrentCell property to a DataGridCellInfo and call BeginEdit() if you want to enter the edit mode programmatically:如果要以编程方式进入编辑模式,请将CurrentCell属性设置为DataGridCellInfo并调用BeginEdit()

private void btnEdit_Click(object sender, RoutedEventArgs e)
{
    MyDatagrid.IsReadOnly = false;
    MyDatagrid.CurrentCell = new DataGridCellInfo((sender as Button).DataContext, dataGrid.Columns[0]);
    MyDatagrid.BeginEdit();
}

Note that you have to set IsReadOnly to false to be able to enter the edit mode.请注意,您必须将IsReadOnly设置为false才能进入编辑模式。 You cannot edit a read-only DataGrid .您不能编辑只读DataGrid You may set it back to true in the CellEditEnding event handler.您可以在CellEditEnding事件处理程序中将其设置回true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM