背景
在很多的时候我们需要编辑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>