Skip to main content
All docs
V24.2

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

Custom AI-powered Extensions

  • 2 minutes to read

The following example creates a custom AI-powered extension (AuthoredStyleExtension) and behavior (AuthoredStyleBehavior).

Custom AI-powered Extension - WPF Text Editor, DevExpress

Run Demo

#Create an Extension

The AuthoredStyleExtension is based on the DevExpress ChangeStyleExtension. The example overrides the GetSystemPrompt method to customize the prompt:

C#
public class AuthoredStyleExtension : ChangeTextExtension<AuthoredStyleRequest> {
    public AuthoredStyleExtension(IServiceProvider serviceProvider) : base(serviceProvider) { }

    protected override string GetSystemPrompt(AuthoredStyleRequest request) =>
        $"Rewrite this text in the {request.Author} style";
}

public class AuthoredStyleRequest : TextRequest {
    public AuthoredStyleRequest(string author, string text) : base(text) {
        Author = author;
    }
    public string Author { get; set; }
}

#Create a Custom Behavior

The AuthoredStyleBehavior is based on the DevExpress TextModificationBehavior (the base class for ChangeStyleBehavior, ChangeToneBehavior, etc.):

C#
class AuthoredStyleBehavior : TextModificationBehavior {
    static AuthoredStyleBehavior() {
        RegisterBehaviorSource<TextEdit, AuthoredStyleBehavior>(te => new TextEditBehaviorSource(te));
        AIExtensionsContainerDesktop.Default.Register<AuthoredStyleRequest, AuthoredStyleExtension>();
    }

    public AuthoredStyleBehavior() {
        AuthoredStyleCommand = CreateCommand<string>(AuthoredStyleAsync);
    }

    ICommand AuthoredStyleCommand { get; set; }

    Task AuthoredStyleAsync(string author) => ExecuteAsync(
        input => new AuthoredStyleRequest(author, input),
        (container, request, token) => {
            var extension = (AuthoredStyleExtension)container.GetExtension(typeof(AuthoredStyleRequest));
            var result = extension.ExecuteAsync(request, token);
            return result;
        });

    protected override ItemInfo BuildItem() {
        return new ItemInfo {
            Caption = "Authored Style",
            Items = new[] { "Mark Twain", "Ernest Hemingway", "Maya Angelou" }.Select(author => new ItemInfo {
                Caption = author,
                Command = AuthoredStyleCommand,
                CommandParameter = author
            }).ToList()
        };
    }
}

#Apply a Custom Extension/Behavior to a Text Editor

xml
<dxe:TextEdit HorizontalAlignment="Left"
              VerticalContentAlignment="Top"
              VerticalScrollBarVisibility="Auto"
              TextWrapping="Wrap" Width="661">
              <dxmvvm:Interaction.Behaviors>
                  <local:AuthoredStyleBehavior/>
              </dxmvvm:Interaction.Behaviors>
</dxe:TextEdit>