Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

datagrid - Is GridView in WPF support row Edit function

I hope to add row edit funtion to gridview in WPF, the code snippet is as following:

<ListView Margin="10" Name="lvUsers" Grid.Row="1">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
            <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}"/>
            <GridViewColumn Header="Mail" Width="170">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Mail}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Grade" Width="60" DisplayMemberBinding="{Binding Grade}" />
        </GridView>
    </ListView.View>
</ListView>

I have seen some infos about DataGrid, but if there exist any method to make row edit funtion for gridview, thanks a lot.

question from:https://stackoverflow.com/questions/66057412/is-gridview-in-wpf-support-row-edit-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The simplest way is to change TextBlock to TextBox.

<DataTemplate>
    <TextBox Text="{Binding Mail}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />
</DataTemplate>

Better way is to use DataGrid which has CellTemplate and CellEditingTemplate

<DataGrid>
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Mail">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Mail}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Mail}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...