Smart Paste
- 9 minutes to read
“SmartPaste” is an AI-powered feature that transforms the traditional copy-and-paste process into a smarter, more efficient tool. Designed to improve productivity, SmartPaste analyzes the content you copy and intelligently assigns the right values to the appropriate fields or row cells in the WPF Data Grid, TreeList, and LayoutControl-driven forms.
Applies To
-
Supported views: TableView, CardView, TreeListView
- Layout Control
- TreeList
How It Works
DevExpress UI controls seamlessly integrate SmartPaste. When SmartPaste is activated, the “Smart Paste” command is automatically added to a control’s popup menu. You can also define a shortcut to invoke SmartPaste.
When you copy data from a source (such as a spreadsheet, document, or web page) and paste it into a Data Grid, TreeList, or LayoutControl-driven form, SmartPaste automatically interprets the content and maps the data to the correct data fields or cells.
Activate SmartPaste
Install DevExpress NuGet Packages
DevExpress.AIIntegration.Wpf
DevExpress.Wpf
Read the following help topics for information on how to obtain the DevExpress NuGet Feed and install DevExpress NuGet packages:
- Choose Between Offline and Online DevExpress NuGet Feeds
- Install NuGet Packages in Visual Studio, VS Code, and Rider
Register AI Client
See the following help topic for information on required NuGet packages and system requirements: Register an AI Client.
The following code snippet registers an Azure OpenAI client at application startup within the AIExtensionsContainerDesktop container:
using Azure.AI.OpenAI;
using DevExpress.AIIntegration;
using DevExpress.Xpf.Core;
using Microsoft.Extensions.AI;
using System;
using System.Windows;
namespace AIAssistantApp {
public partial class App : Application {
static App() {
CompatibilitySettings.UseLightweightThemes = true;
}
protected override void OnStartup(StartupEventArgs e) {
base.OnStartup(e);
ApplicationThemeHelper.ApplicationThemeName = "Win11Light";
// For example, ModelId = "gpt-4o-mini"
IChatClient azureChatClient = new Azure.AI.OpenAI.AzureOpenAIClient(new Uri(AzureOpenAIEndpoint),
new System.ClientModel.ApiKeyCredential(AzureOpenAIKey)).GetChatClient(ModelId).AsIChatClient();
AIExtensionsContainerDesktop.Default.RegisterChatClient(azureChatClient);
}
}
}
Attach SmartPaste Behavior
Attach the SmartPasteBehavior to a control and describe items so that SmartPaste can assign the right values to appropriate items. In the context of SmartPaste, an item refers to a LayoutItem when working with a LayoutControl, or a GridColumn / TreeListColumn when working with a GridControl or TreeListControl.
Tip
You can also use a control’s smart tag menu to attach the SmartPasteBehavior
at design time.
Example: Grid Control
The following example attaches the SmartPasteBehavior
to a grid control. In this example, the SmartPaste operation updates cell values in the focused row.
Note
The TableView.PasteMode/TreeListView.PasteMode property must be set to PasteMode.Append
or PasteMode.Update
. Otherwise, the popup menu’s “Smart Paste” command is disabled.
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
xmlns:dxai="http://schemas.devexpress.com/winfx/2008/xaml/ai"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
<dxg:GridControl x:Name="gridControl" ItemsSource="{Binding Items}">
<dxg:GridControl.View>
<dxg:TableView PasteMode="Update"/>
</dxg:GridControl.View>
<dxmvvm:Interaction.Behaviors>
<dxai:SmartPasteBehavior x:Name="smartPasteBehavior"/>
</dxmvvm:Interaction.Behaviors>
<dxg:GridColumn FieldName="OrderID"
dxai:SmartPasteBehavior.ExcludeItem="True"/>
<dxg:GridColumn FieldName="OrderDate"
dxai:SmartPasteBehavior.ItemDescription="The date when an order was placed."/>
<dxg:GridColumn FieldName="ShippingDate"
dxai:SmartPasteBehavior.ItemDescription="The date when an order is scheduled to be shipped."/>
<dxg:GridColumn FieldName="Shipped"
dxai:SmartPasteBehavior.ItemDescription="Indicates whether an order has been shipped. If 1 use True. If -1 use False."/>
</dxg:GridControl>
using DevExpress.AIIntegration.Wpf;
using DevExpress.Mvvm;
using DevExpress.Mvvm.UI.Interactivity;
using DevExpress.Xpf.Grid;
using System;
using System.ComponentModel;
public partial class MainWindow {
BindingList<Order> ds;
GridControl gridControl;
SmartPasteBehavior smartPasteBehavior;
public MainWindow() {
InitializeComponent();
ds = new BindingList<Order>() {
new Order(){ OrderID = 14243684, OrderDate = DateTime.Today, ShippingDate = DateTime.Now.AddDays(4), Shipped = false}
};
smartPasteBehavior = new SmartPasteBehavior();
gridControl = new GridControl() {
ItemsSource = ds,
View = new TableView() { PasteMode = DevExpress.Export.PasteMode.Update }
};
gridControl.Columns.Add(new GridColumn() { FieldName = "OrderID" });
gridControl.Columns.Add(new GridColumn() { FieldName = "OrderDate" });
gridControl.Columns.Add(new GridColumn() { FieldName = "ShippingDate" });
gridControl.Columns.Add(new GridColumn() { FieldName = "Shipped" });
Interaction.GetBehaviors(gridControl).Add(smartPasteBehavior);
SmartPasteBehavior.SetExcludeItem(gridControl.Columns["OrderID"], true);
SmartPasteBehavior.SetItemDescription(gridControl.Columns["OrderDate"], "The date when an order was placed.");
SmartPasteBehavior.SetItemDescription(gridControl.Columns["ShippingDate"], "The date when an order is scheduled to be shipped.");
SmartPasteBehavior.SetItemDescription(gridControl.Columns["Shipped"], "Indicates whether an order has been shipped. If 1 use True. If -1 use False.");
mainGrid.Children.Add(gridControl);
}
}
public class Order : BindableBase {
public int OrderID { get; set; }
public DateTime OrderDate { get; set; }
public DateTime ShippingDate { get; set; }
public bool Shipped { get; set; }
}
Example: Layout Control
This example attaches the SmartPasteBehavior
to a Layout Control:
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxai="http://schemas.devexpress.com/winfx/2008/xaml/ai"
<dxlc:LayoutControl x:Name="layoutControl" Grid.Column="1" MaxWidth="800">
<dxmvvm:Interaction.Behaviors>
<dxai:SmartPasteBehavior x:Name="lcSmartPasteBehavior"/>
</dxmvvm:Interaction.Behaviors>
<dxlc:LayoutGroup Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Top">
<dxlc:LayoutGroup View="GroupBox" Header="Billing Address" Orientation="Vertical">
<dxlc:LayoutItem x:Name="addressLine1" Label="Line 1"
dxai:SmartPasteBehavior.ItemDescription="The primary address line, typically containing the street number and name (for example, 123 Main St). Supports both letters and numbers.">
<dxe:TextEdit/>
</dxlc:LayoutItem>
<dxlc:LayoutItem x:Name="addressLine2" Label="Line 2"
dxai:SmartPasteBehavior.ItemDescription="An optional address line that may include an apartment, suite, or unit number (for example, Apt 4B).Supports both letters and numbers.">
<dxe:TextEdit/>
</dxlc:LayoutItem>
<dxlc:LayoutItem x:Name="addressCity" Label="City">
<dxe:TextEdit/>
</dxlc:LayoutItem>
<dxlc:LayoutGroup View="Group" Orientation="Horizontal">
<dxlc:LayoutItem x:Name="addressRegion" Label="State/Province/Region">
<dxe:TextEdit/>
</dxlc:LayoutItem>
<dxlc:LayoutItem x:Name="addressZip" Label="ZIP">
<dxe:TextEdit/>
</dxlc:LayoutItem>
</dxlc:LayoutGroup>
</dxlc:LayoutGroup>
</dxlc:LayoutGroup>
</dxlc:LayoutControl>
using DevExpress.AIIntegration.Wpf;
using DevExpress.Mvvm.UI.Interactivity;
using DevExpress.Xpf.Editors;
using DevExpress.Xpf.LayoutControl;
namespace DXSmartPaste {
public partial class MainWindow {
LayoutControl layoutControl;
public MainWindow() {
InitializeComponent();
layoutControl = new LayoutControl();
SmartPasteBehavior smartPasteBehavior = new SmartPasteBehavior();
Interaction.GetBehaviors(layoutControl).Add(smartPasteBehavior);
LayoutGroup addressGroup = new LayoutGroup() {
View = LayoutGroupView.GroupBox,
Header = "Billing Address",
};
LayoutItem addressLine1LayoutItem = new LayoutItem() { Label = "Line 1", Content = new TextEdit() };
LayoutItem addressLine2LayoutItem = new LayoutItem() { Label = "Line 2", Content = new TextEdit() };
addressGroup.Children.Add(addressLine1LayoutItem);
addressGroup.Children.Add(addressLine2LayoutItem);
LayoutGroup personalInfoGroup = new LayoutGroup() {
View = LayoutGroupView.GroupBox,
Header = "Personal Info"
};
LayoutItem nameLayoutItem = new LayoutItem() { Label = "Name", Content = new TextEdit() };
LayoutItem lastNameLayoutItem = new LayoutItem() { Label = "Last Name", Content = new TextEdit() };
personalInfoGroup.Children.Add(nameLayoutItem);
personalInfoGroup.Children.Add(lastNameLayoutItem);
layoutControl.Children.Add(addressGroup);
layoutControl.Children.Add(personalInfoGroup);
SmartPasteBehavior.SetItemDescription(addressLine1LayoutItem,
"The primary address line, typically containing the street number and name (for example, 123 Main St). Supports both letters and numbers.");
SmartPasteBehavior.SetItemDescription(addressLine2LayoutItem,
"An optional address line that may include an apartment, suite, or unit number (for example, Apt 4B).Supports both letters and numbers.");
SmartPasteBehavior.SetExcludeItem(personalInfoGroup, true);
mainGrid.Children.Add(layoutControl);
}
}
}
SmartPaste Settings
Item Descriptions
Note
Item descriptions are optional if a layout item’s Label
or a column’s FieldName
property is set to a non-empty string. Although SmartPaste attempts to determine the right value for an item based on the item’s name or field name, we recommend that you also describe items for improved accuracy.
Use the ItemDescription attached property or SetItemDescription method to specify a string that describes the expected content and/or data format for the item.
<dxg:GridColumn FieldName="ShippingDate"
dxai:SmartPasteBehavior.ItemDescription="The date when an order is scheduled to be shipped."/>
When pasting into data fields of basic data types (such as Boolean, date-time, numeric, string), SmartPaste automatically appends default formatting prompts to item descriptions. You can use item descriptions to specify custom formatting prompts as needed.
For example, if your data source designates Boolean values as 1 and -1, you can specify the following item description: “If 1 use True. If -1 use False.”
<dxg:GridColumn FieldName="Shipped"
dxai:SmartPasteBehavior.ItemDescription="If 1 use True. If -1 use False."/>
Excluded Items
Use the ExcludeItem attached property or SetExcludeItem method to exclude a specific item from SmartPaste. This allows you to exclude columns or layout items from being affected by SmartPaste, ensuring that they remain unchanged.
<dxg:GridColumn FieldName="OrderID"
dxai:SmartPasteBehavior.ExcludeItem="True"/>
Tip
In the Layout Control, apply the ExcludedItem
to a LayoutGroup
to exclude all items within the group:
<dxlc:LayoutGroup View="GroupBox"
dxai:SmartPasteBehavior.ExcludeItem="True">
...
</dxlc:LayoutGroup>
SmartPaste API
Method | Description |
---|---|
SmartPasteBehavior.SmartPasteAsync() | Assigns the right values (from the clipboard) to the appropriate items. |
SmartPasteBehavior.SmartPasteAsync(string inputText) | Assigns the right values from the specified string to the appropriate items. |
Invoke SmartPaste Using a Shortcut
The following code snippet invokes SmartPaste when a user presses Ctrl+P:
<dxmvvm:Interaction.Behaviors>
<dxai:SmartPasteBehavior x:Name="smartPasteBehavior"/>
<dxmvvm:KeyToCommand KeyGesture="CTRL+P" Command="{Binding SmartPasteCommand, ElementName=smartPasteBehavior}"/>
</dxmvvm:Interaction.Behaviors>
Handle SmartPaste Operations
You can validate a SmartPaste result to ensure that the data adheres to predefined rules or formats required by your application. Use built-in APIs of DevExpress controls to analyze, and if necessary, modify the data returned by SmartPaste.
Grid and TreeList
The following events allow you to cancel a SmartPaste operation based on a specific condition or to modify the result base on your preferences:
- TableView.ClipboardRowPasting / TreeListView.ClipboardRowPasting
- TableView.ClipboardRowCellValuePasting / TreeListView.ClipboardRowCellValuePasting
- DataControlBase.PastingFromClipboard
Refer to the following help topic for additional information and examples: Paste Operations.
Layout Control
For data editors displayed within layout items, handle the LayoutControl.SmartPasteEditValueChanging event to analyze and modify SmartPaste results, or cancel the operation.
Data Grid, TreeList, and Layout Control Specifics
SmartPaste in Data Grid and TreeList
- Supported Views
- View Requirements
- Clipboard paste operations must be allowed. The View’s
PasteMode
property should be set toPasteMode.Append
orPasteMode.Update
. - Column Requirements
- The column must be visible. The column’s
Visible
property must be set to true. - The column must not be read-only.
- Multi-select data editors (for example,
ComboBoxEdit
andLookUpEdit
in token mode) do not support SmartPaste.
- The column must be visible. The column’s
- Value Conversion
Values are pasted into grid cells as strings. The grid automatically converts string values to appropriate data types, which include:
- Numeric Types:
int
,uint
,short
,ushort
,float
,double
,decimal
, etc. - Date-Time Types:
DateTime
,DateOnly
,TimeOnly
. - Boolean
Important
SmartPaste does not support nullable types.
- Numeric Types:
- Master-Detail Mode
- Smart Paste is not supported in the Master-Detail mode (when
DataControlDetailDescriptor
is used).
SmartPaste in Layout Control
- Layout Item and Layout Group Requirements
- The layout item must contain a data editor (for example, TextEdit, DateEdit, SpinEdit, etc.). The editor must be visible and enabled. Multi-select data editors (for example,
ComboBoxEdit
andLookUpEdit
in token mode) do not support SmartPaste. - The layout item must have the specified Label and/or
ItemDescription
. - The layout item must be enabled. The layout item’s
IsEnabled
property is set to true. - The layout item must be visible. The layout item’s
IsVisible
property is set to true. - The
DataLayoutItem
must not be read-only. - The layout group must be visible, expanded, and enabled.
- The layout item must contain a data editor (for example, TextEdit, DateEdit, SpinEdit, etc.). The editor must be visible and enabled. Multi-select data editors (for example,
- Layout Customization Mode
- SmartPaste is disabled in Customization Mode.