Click here to Skip to main content
15,886,258 members
Articles / Programming Languages / C#

Embedding a Console in a C# Application

Rate me:
Please Sign up or sign in to vote.
4.91/5 (151 votes)
21 Mar 2015MIT4 min read 622.3K   27.7K   403  
Embed a functional console window in a C# application
<Window x:Class="ConsoleControlSample.WPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:consoleControl="clr-namespace:ConsoleControl.WPF;assembly=ConsoleControl.WPF"
        xmlns:local="clr-namespace:ConsoleControlSample.WPF"
        xmlns:apexConverters="clr-namespace:Apex.Converters;assembly=Apex"
        Title="Console Control WPF Sample" Height="400" Width="600">
    
    <!-- The view model. -->
    <Window.DataContext>
        <local:MainViewModel x:Name="viewModel" />
    </Window.DataContext>
    
    <!-- Resources.-->
    <Window.Resources>
        <apexConverters:InvertedBooleanConverter x:Key="InvertedBooleanConverter" />
    </Window.Resources>
    <Grid>
        <DockPanel>
            
            <!-- The main toolbar. -->
            <ToolBarTray 
                DockPanel.Dock="Top">
                <ToolBar>
                    <Button 
                        Command="{Binding StartCommandPromptCommand}" 
                        IsEnabled="{Binding IsProcessRunning, ElementName=consoleControl, 
                        Converter={StaticResource InvertedBooleanConverter}}">
                        <Image Source="/ConsoleControlSample.WPF;component/Resources/ConsoleControl.png"
                                RenderOptions.BitmapScalingMode="Linear" Width="16" Height="16" />
                    </Button>
                    <Separator />
                    <Button Command="{Binding StartNewProcessCommand}"
                        IsEnabled="{Binding IsProcessRunning, ElementName=consoleControl, 
                        Converter={StaticResource InvertedBooleanConverter}}">
                        <Image Source="/ConsoleControlSample.WPF;component/Resources/Play.png" 
                                RenderOptions.BitmapScalingMode="Linear" Width="16" Height="16" />
                    </Button>
                    <Button Command="{Binding StopProcessCommand}"
                        IsEnabled="{Binding IsProcessRunning, ElementName=consoleControl, 
                        Converter={StaticResource InvertedBooleanConverter}, ConverterParameter=Invert}">
                        <Image Source="/ConsoleControlSample.WPF;component/Resources/Stop.png"
                                RenderOptions.BitmapScalingMode="Linear" Width="16" Height="16"  />
                    </Button>
                    <Separator />
                    <ToggleButton IsChecked="{Binding ShowDiagnostics, ElementName=consoleControl}">
                        <Image Source="/ConsoleControlSample.WPF;component/Resources/Information.png"
                                RenderOptions.BitmapScalingMode="Linear" Width="16" Height="16"  />
                    </ToggleButton>
                    <ToggleButton IsChecked="{Binding IsInputEnabled, ElementName=consoleControl}">
                        <Image Source="/ConsoleControlSample.WPF;component/Resources/Control_TextBox.png" 
                                RenderOptions.BitmapScalingMode="Linear" Width="16" Height="16" />
                    </ToggleButton>
                    <ToggleButton IsChecked="{Binding EnableKeyboardCommands}">
                        <Image Source="/ConsoleControlSample.WPF;component/Resources/GotoShortcuts.png" 
                                RenderOptions.BitmapScalingMode="Linear" Width="16" Height="16" />
                    </ToggleButton>
                    <Button Command="{Binding ClearOutputCommand}">
                        <Image Source="/ConsoleControlSample.WPF;component/Resources/Delete.png" 
                                RenderOptions.BitmapScalingMode="Linear" Width="16" Height="16" />
                    </Button>
                </ToolBar>
            </ToolBarTray>
            
            <!-- The Status Bar. -->
            <StatusBar DockPanel.Dock="Bottom">
                <TextBlock Text="Console State:" />
            </StatusBar>
        <consoleControl:ConsoleControl 
            x:Name="consoleControl" />
        </DockPanel>
    </Grid>
</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 MIT License


Written By
Software Developer
United Kingdom United Kingdom
Follow my blog at www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.

Comments and Discussions