Click here to Skip to main content
15,881,424 members
Articles / Desktop Programming / WPF

Using AvalonEdit (WPF Text Editor)

Rate me:
Please Sign up or sign in to vote.
4.97/5 (271 votes)
1 Apr 2013LGPL313 min read 1.8M   72.3K   534  
AvalonEdit is an extensible Open-Source text editor with support for syntax highlighting and folding.
<!-- 
	Copyright (c) 2009 Daniel Grunwald
	
	Permission is hereby granted, free of charge, to any person obtaining a copy of this
	software and associated documentation files (the "Software"), to deal in the Software
	without restriction, including without limitation the rights to use, copy, modify, merge,
	publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
	to whom the Software is furnished to do so, subject to the following conditions:
	
	The above copyright notice and this permission notice shall be included in all copies or
	substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
	INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
	PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
	FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
	OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
	DEALINGS IN THE SOFTWARE.
-->
<Window x:Class="AvalonEdit.Sample.Window1"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
	xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
	TextOptions.TextFormattingMode="Display"
	Title="AvalonEdit.Sample" Height="500" Width="700"
	>
	<DockPanel>
		<ToolBar DockPanel.Dock="Top">
			<ToolBar.Resources>
				<Style TargetType="{x:Type Image}">
					<Style.Triggers>
						<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ButtonBase}, AncestorLevel=1}, Path=IsEnabled}" Value="False">
							<Setter Property="Opacity" Value="0.30" />
						</DataTrigger>
					</Style.Triggers>
				</Style>
			</ToolBar.Resources>
			<Button Click="openFileClick"><Image Source="Images/Open.png" Height="16"/></Button>
			<Button Click="saveFileClick"><Image Source="Images/Save.png" Height="16"/></Button>
			<Separator/>
			<Button Command="Cut"><Image Source="Images/Cut.png" Height="16"/></Button>
			<Button Command="Copy"><Image Source="Images/Copy.png" Height="16"/></Button>
			<Button Command="Paste"><Image Source="Images/Paste.png" Height="16"/></Button>
			<Button Command="Delete"><Image Source="Images/Delete.png" Height="16"/></Button>
		    <Separator/>
			<Button Command="Undo"><Image Source="Images/Undo.png" Height="16"/></Button>
			<Button Command="Redo"><Image Source="Images/Redo.png" Height="16"/></Button>
			<Separator/>
			<CheckBox IsChecked="{Binding ElementName=textEditor,Path=WordWrap}">
				<Image Source="Images/WordWrap.png" Height="16"/>
			</CheckBox>
			<CheckBox IsChecked="{Binding ElementName=textEditor,Path=ShowLineNumbers}">
				<TextBlock Width="16" TextAlignment="Center">#</TextBlock>
			</CheckBox>
			<CheckBox IsChecked="{Binding ElementName=textEditor,Path=Options.ShowEndOfLine}">
				<TextBlock Width="16" TextAlignment="Center">¶</TextBlock>
			</CheckBox>
			<ComboBox Name="highlightingComboBox"
				SelectedItem="{Binding SyntaxHighlighting, ElementName=textEditor}"
				ItemsSource="{Binding Source={x:Static avalonEdit:HighlightingManager.Instance}, Path=HighlightingDefinitions}"
				SelectionChanged="HighlightingComboBox_SelectionChanged"/>
		</ToolBar>
		<Grid>
			<Grid.ColumnDefinitions>
				<ColumnDefinition Width="1*"/>
				<ColumnDefinition Width="100"/>
			</Grid.ColumnDefinitions>
			<avalonEdit:TextEditor
				Name="textEditor"
				FontFamily="Consolas"
				FontSize="10pt"
				SyntaxHighlighting="C#"
			>Welcome to AvalonEdit!
			</avalonEdit:TextEditor>
			<GridSplitter Grid.Column="1" Width="4" HorizontalAlignment="Left"/>
			<DockPanel Grid.Column="1" Margin="4 0 0 0">
				<ComboBox Name="propertyGridComboBox" DockPanel.Dock="Top"
				          SelectedIndex="0" SelectionChanged="propertyGridComboBoxSelectionChanged">
					<ComboBoxItem>TextEditor</ComboBoxItem>
					<ComboBoxItem>TextArea</ComboBoxItem>
					<ComboBoxItem>Options</ComboBoxItem>
				</ComboBox>
				<WindowsFormsHost DockPanel.Dock="Right" Name="propertyGridHost">
					<forms:PropertyGrid x:Name="propertyGrid"/>
				</WindowsFormsHost>
			</DockPanel>
		</Grid>
	</DockPanel>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Germany Germany
I am the lead developer on the SharpDevelop open source project.

Comments and Discussions