• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

c# WPF中通过双击编辑DataGrid中Cell的示例(附源码)

c# 搞代码 4年前 (2022-01-09) 44次浏览 已收录 0个评论
文章目录[隐藏]

背景

在很多的时候我们需要编辑DataGrid中每一个Cell,编辑后保存数据,原生的WPF中的DataGrid并没有提供这样的功能,今天通过一个具体的例子来实现这一个功能,在这个例子中DataGrid中的数据类型可能是多种多样的,有枚举、浮点类型、布尔类型、DateTime类型,每一种不同的类型需要双击以后呈现不同的效果,本文通过使用Xceed.Wpf.DataGrid这个动态控件库来实现这个功能,当前使用的Dll版本是2.5.0.0,不同的版本可能实现上面有差别,这个在使用的时候需要特别注意。

Demo预览

代码结构

代码还是按照常规的MVVM结构来进行编写,主要包括Views、Models、MainWindowViewModel、Converters这些常规的结构,在介绍这些之前来说一下我们的整体结构,在Demo中我们准备了一个四行三列的DataGrid,其中第一列主要是表示当前行的名称、后面的Step列是根据代码动态进行添加的,这个Step部分是我们通过代码动态调整的,调整完成后能够将数据保存到数据源中,我们还是按照MVVM的结构来进行进行代码的介绍。

  1 MainWindow

<Window x:Class="DataGridCellDoubleClickDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:xceed="http://schemas.xceed.com/wpf/xaml/datagrid"
        xmlns:models="clr-namespace:DataGridCellDoubleClickDemo.Models"
        xmlns:views="clr-namespace:DataGridCellDoubleClickDemo.Views"
        mc:Ignorable="d"
        Title="DataGridDemo" Height="450" Width="800">
    <Window.Resources>
        <DataTemplate x:Key="CustomTemplate">
            <Border BorderThickness="1" BorderBrush="Blue">
                <TextBlock Text="{Binding Path=Display }"  FontWeight="Bold"
                           HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
            </Border>
        </DataTemplate>
        <DataTemplate x:Key="RowHeadTemplate" DataType="{x:Type models:RecipeControlVariable}">
            <TextBlock Text="{Binding DisplayName}"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Foreground="Black" FontSize="12"/>
        </DataTemplate>
        <xceed:DataGridCollectionViewSource x:Key="recipeData" Source="{Binding RecipeVariables}"></xceed:DataGridCollectionViewSource>
    </Window.Resources>
    <Grid>
        <xceed:DataGridControl x:Name="DataGridControl"
                               AutoCreateColumns="False"
                               FontSize="13"
                               VerticalContentAlignment="Center"
                               BorderBrush="Gray"
                               BorderThickness="1"
                               ItemsPrimaryAxis="Horizontal"
                               PagingBehavior="LeftToRight"
                               UpdateSourceTrigger="CellContentChanged"
                               SelectionUnit="Cell"
                               SelectionMode="Single"                              
                               ItemsSource="{Binding  Source={StaticResource recipeData}}">
            <xceed:DataGridControl.View>
                <xceed:TableflowView FixedColumnCount="1" ContainerHeight="30" x:Name="tblView"
                                        VerticalGridLineThickness="0.5" HorizontalGridLineBrush="Gray"
                                        HorizontalGridLineThickness="1" VerticalGridLineBrush="Black"
                                        RowFadeInAnimationDuration="0"
                                        ScrollingAnimationDuration="0" ColumnStretchMinWidth="10"
                                        DetailIndicatorWidth="20" ShowRowSelectorPane="False"
                                        ShowScrollTip="False" UseDefaultHeadersFooters="False">
                    <xceed:TableflowView.FixedHeaders>
                        <DataTemplate>
                            <xceed:ColumnManagerRow AllowColumnReorder="False" AllowColumnResize="True" AllowDrop="False" AllowSort="False" />
                        </DataTemplate>
                    </xceed:TableflowView.FixedHeaders>
                </xceed:TableflowView>
            </xceed:DataGridControl.View>
 
            <xceed:DataGridControl.DefaultCellEditors>
                <xceed:CellEditor x:Key="{x:Type models:SmartCellViewModel}">
                    <xceed:CellEditor.EditTemplate>
     &<strong>本文来源gaodai#ma#com搞@@代~&码网</strong>nbsp;                  <DataTemplate>
                            <views:SmartCellEditor Content="{xceed:CellEditorBinding}"  VerticalAlignment="Center"
                                                   Height="{Binding ActualHeight,RelativeSource={RelativeSource AncestorType={x:Type Border},AncestorLevel=1}}"></views:SmartCellEditor>
                        </DataTemplate>
                    </xceed:CellEditor.EditTemplate>
                </xceed:CellEditor>
            </xceed:DataGridControl.DefaultCellEditors>
 
        </xceed:DataGridControl>
    </Grid>
</Window>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:c# WPF中通过双击编辑DataGrid中Cell的示例(附源码)
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址