Skip to content

Commit 38812d9

Browse files
authored
Merge pull request #1 from SyncfusionExamples/985360
985360: Updated ReadMe file
2 parents 7cb31d5 + 5d7d6ec commit 38812d9

File tree

1 file changed

+100
-2
lines changed

1 file changed

+100
-2
lines changed

README.md

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,103 @@
1-
# How to configure custom language using WPF Syntax Editor?
1+
# How to Configure a Custom Language in WPF Syntax Editor
2+
This example demonstrates how to configure and apply a custom language definition (Python) in the Syncfusion WPF Syntax Editor control. The Syntax Editor supports syntax highlighting, code editing, and parsing for various languages. You can define your own language grammar using LexemCollection and FormatsCollection for custom syntax rules.
23

3-
This sample holds the configuration of custom language - IronPython applied and loaded using [Syntax Editor](https://help.syncfusion.com/wpf/syntax-editor/getting-started).
4+
## Why This Is Useful
5+
- **Custom Language Support**: Add domain-specific or scripting languages.
6+
- **Syntax Highlighting**: Highlight keywords, comments, operators, and literals.
7+
- **MVVM Integration**: Apply language configuration dynamically at runtime.
48

9+
## Key Steps
10+
- Define _FormatsCollection_ for syntax highlighting styles.
11+
- Define _LexemCollection_ for language tokens and rules.
12+
- Apply the custom language to the EditControl using the _CustomLanguage_ property.
13+
14+
## Code Example
15+
**XAML**
16+
```XAML
17+
<Window.DataContext>
18+
<local:CustomLanguageViewModel />
19+
</Window.DataContext>
20+
21+
<Window.Resources>
22+
<!-- Define syntax highlighting formats -->
23+
<syncfusion:FormatsCollection x:Key="pythonLanguageFormats">
24+
<syncfusion:EditFormats Foreground="Green" FormatName="CommentFormat" />
25+
<syncfusion:EditFormats Foreground="Black" FormatName="MultilineCommentFormat" />
26+
<syncfusion:EditFormats Foreground="Blue" FormatName="KeywordFormat" />
27+
<syncfusion:EditFormats Foreground="Navy" FormatName="OperatorFormat" />
28+
<syncfusion:EditFormats Foreground="Gray" FormatName="LiteralsFormat" />
29+
</syncfusion:FormatsCollection>
30+
31+
<!-- Define language tokens -->
32+
<syncfusion:LexemCollection x:Key="pythonLanguageLexems">
33+
<syncfusion:Lexem ContainsEndText="True"
34+
EndText="\r\n"
35+
IntellisenseDisplayText="class"
36+
IsMultiline="True"
37+
IsRegex="True"
38+
LexemType="CodeSnippet"
39+
ScopeLevel="Class"
40+
StartText="class \w+[\s:\w,()]+" />
41+
<syncfusion:Lexem ContainsEndText="False"
42+
FormatName="OperatorFormat"
43+
IsMultiline="False"
44+
LexemType="Operator"
45+
StartText="**;=" />
46+
</syncfusion:LexemCollection>
47+
</Window.Resources>
48+
49+
<!-- Syntax Editor -->
50+
<syncfusion:EditControl Name="editControl"
51+
DocumentLanguage="{Binding Language}"
52+
DocumentSource="{Binding DocumentSource}"
53+
FontFamily="{Binding SelectedFont}"
54+
FontSize="14">
55+
<interactivity:Interaction.Triggers>
56+
<interactivity:EventTrigger EventName="Loaded">
57+
<interactivity:InvokeCommandAction Command="{Binding EditLoadedCommand}"
58+
CommandParameter="{Binding ElementName=editControl}" />
59+
</interactivity:EventTrigger>
60+
</interactivity:Interaction.Triggers>
61+
</syncfusion:EditControl>
62+
```
63+
**ViewModel (C#)**
64+
```C#
65+
public class CustomLanguageViewModel : NotificationObject
66+
{
67+
public ICommand EditLoadedCommand { get; }
68+
public string DocumentSource { get; set; }
69+
public Languages Language { get; set; }
70+
public FontFamily SelectedFont { get; set; }
71+
72+
public CustomLanguageViewModel()
73+
{
74+
DocumentSource = @"Data\syntaxeditor\PythonSource.py";
75+
SelectedFont = new FontFamily("Verdana");
76+
Language = Languages.Custom;
77+
EditLoadedCommand = new DelegateCommand<object>(ExecuteEditLoaded);
78+
}
79+
80+
private void ExecuteEditLoaded(object obj)
81+
{
82+
var editControl = obj as EditControl;
83+
var customLanguage = new PythonLanguage(editControl)
84+
{
85+
Lexem = (LexemCollection)Application.Current.Resources["pythonLanguageLexems"],
86+
Formats = (FormatsCollection)Application.Current.Resources["pythonLanguageFormats"]
87+
};
88+
editControl.CustomLanguage = customLanguage;
89+
}
90+
}
91+
```
92+
93+
## Notes
94+
- Requires Syncfusion WPF Syntax Editor NuGet package.
95+
- You can extend this approach for other languages or domain-specific syntax.
96+
- Use LexemCollection for token definitions and FormatsCollection for styling.
97+
98+
## Output
599
![Syntax Editor with custom language](Images/output.png)
100+
101+
## Reference
102+
For more details refer the Syncfusion WPF Syntax Editor Documentation for [custom language](https://help.syncfusion.com/wpf/syntax-editor/language-support/custom-language-support) support.
103+

0 commit comments

Comments
 (0)