wpf-405369-ai-powered-extensions-smart-paste.md
“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.
Run Demo: Smart Paste - Data Grid
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.
DevExpress.AIIntegration.WpfDevExpress.WpfRead the following help topics for information on how to obtain the DevExpress NuGet Feed and install DevExpress NuGet packages:
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);
}
}
}
Imports Azure.AI.OpenAI
Imports DevExpress.AIIntegration
Imports DevExpress.Xpf.Core
Imports Microsoft.Extensions.AI
Imports System
Imports System.Windows
Namespace AIAssistantApp
Partial Public Class App
Inherits Application
Shared Sub New()
CompatibilitySettings.UseLightweightThemes = True
End Sub
Protected Overrides Sub OnStartup(ByVal e As StartupEventArgs)
MyBase.OnStartup(e)
ApplicationThemeHelper.ApplicationThemeName = "Win11Light"
' For example, ModelId = "gpt-4o-mini"
Dim azureChatClient As IChatClient = New AzureOpenAIClient(New Uri(AzureOpenAIEndpoint),
New System.ClientModel.ApiKeyCredential(AzureOpenAIKey)).GetChatClient(ModelId).AsIChatClient()
AIExtensionsContainerDesktop.Default.RegisterChatClient(azureChatClient)
End Sub
End Class
End Namespace
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.
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>
Grid Control: Attach the SmartPaste Behavior in Code Behind
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; }
}
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>
Layout Control: Attach the SmartPaste Behavior in Code Behind
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);
}
}
}
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."/>
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>
Enable the ProcessClipboardImage option to analyze images copied to the clipboard. SmartPaste checks whether the clipboard contains an image and attempts to extract structured data from it using AI.
You can specify additional instructions that modify the default prompt before processing. Prompt augmentation improves the relevance and accuracy of the AI’s response. Use the PromptAugmentation property to specify additional instructions.
The following code snippet registers a SmartPasteBehavior and configures it to always return results in English:
<dxmvvm:Interaction.Behaviors>
<dxai:SmartPasteBehavior x:Name="smartPasteBehavior"
PromptAugmentation="Always translate the result into English."/>
</dxmvvm:Interaction.Behaviors>
| 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. |
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>
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.
The following events allow you to cancel a SmartPaste operation based on a specific condition or to modify the result base on your preferences:
Refer to the following help topic for additional information and examples: Paste Operations.
For data editors displayed within layout items, handle the LayoutControl.SmartPasteEditValueChanging event to analyze and modify SmartPaste results, or cancel the operation.
Supported Views
View RequirementsClipboard paste operations must be allowed. The View’s PasteMode property should be set to PasteMode.Append or PasteMode.Update.Column Requirements
Visible property must be set to true.ComboBoxEdit and LookUpEdit in token mode) do not support SmartPaste.Value Conversion
Values are pasted into grid cells as strings. The grid automatically converts string values to appropriate data types, which include:
int, uint, short, ushort, float, double, decimal, etc.DateTime, DateOnly, TimeOnly.Important
SmartPaste does not support nullable types.
Master-Detail ModeSmart Paste is not supported in the Master-Detail mode (when DataControlDetailDescriptor is used).
Layout Item and Layout Group Requirements
ComboBoxEdit and LookUpEdit in token mode) do not support SmartPaste.ItemDescription.IsEnabled property is set to true.IsVisible property is set to true.DataLayoutItem must not be read-only.Layout Customization ModeSmartPaste is disabled in Customization Mode.