Click here to Skip to main content
15,896,111 members
Articles / Programming Languages / C#

WCF / WPF Chat Application

Rate me:
Please Sign up or sign in to vote.
4.93/5 (478 votes)
22 Jul 2011CPOL29 min read 1.9M   61.3K   1.2K  
How to create a peer-to-peer chat application using Windows Communication Foundation
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:Common;assembly=Common"
    xmlns:local="clr-namespace:WPFChatter"
    
    xmlns:d="http://schemas.microsoft.com/expression/blend/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
    
    x:Class="WPFChatter.Window1"
    
    Title="WPF/WCF Chatter" Height="600" Width="800" Background="#FF2E2E2E"
    WindowStartupLocation="CenterScreen"
    Icon="chat.png"
    MinHeight="600" MinWidth="800"
    ResizeMode="NoResize" ShowInTaskbar="True" SizeToContent="Manual"
    >


  <Window.Resources>

    <!-- Show Chat Window Animation -->
    <Storyboard x:Key="showChatWindow">
      <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ChatControl" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
        <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
        <SplineDoubleKeyFrame KeyTime="00:00:001" Value="1"/>
      </DoubleAnimationUsingKeyFrames>
      <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ChatControl" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
        <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
        <SplineDoubleKeyFrame KeyTime="00:00:001" Value="1"/>
      </DoubleAnimationUsingKeyFrames>
    </Storyboard>


    <!-- Close Chat Window Animation -->
    <Storyboard x:Key="hideChatWindow">
      <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ChatControl" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
        <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
        <SplineDoubleKeyFrame KeyTime="00:00:001" Value="0"/>
      </DoubleAnimationUsingKeyFrames>
      <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ChatControl" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
        <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
        <SplineDoubleKeyFrame KeyTime="00:00:001" Value="0"/>
      </DoubleAnimationUsingKeyFrames>
    </Storyboard>

  </Window.Resources>



  <DockPanel Background="#FF2E2E2E">

    <!-- Header Area-->
    <Border Height="75" Background="#FF000000" DockPanel.Dock="Top" BorderThickness="0,0,0,1" BorderBrush="#FFFFF9F9" >
      <DockPanel x:Name="Header" >
        <StackPanel Width="404" Height="76.14" Background="#FF000000" DockPanel.Dock="Left"  Orientation="Horizontal">
          <Label HorizontalAlignment="Left" Margin="0,26,0,0" VerticalAlignment="Top" Width="62" Height="43" Content="WPF." FontFamily="Agency FB" FontSize="35" Foreground="#FFFDFDFD"/>
          <Label Width="56" Height="43" Content="WCF" FontFamily="Agency FB" FontSize="35" Foreground="#FF999999" HorizontalAlignment="Left" Margin="-10,26,0,0" VerticalAlignment="Top" Background="#00E8E8E8" BorderThickness="0,0,0,0"/>
          <Label Width="289" Height="53" Content="Chat Application" FontFamily="Agency FB" FontSize="35" Foreground="#FFFFFFFF" Background="#00E8E8E8" BorderThickness="0,0,0,0" HorizontalAlignment="Left" Margin="0,26,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
        </StackPanel>
        <StackPanel DockPanel.Dock="Right" Orientation="Vertical" HorizontalAlignment="Right" VerticalAlignment="Top" Width="201" Height="52.4">
          <Label Background="#FF000000" HorizontalAlignment="Right" Margin="0,0,0,0" VerticalAlignment="Top" Width="80.217" Content="By Sacha Barber" FontFamily="Arial" FontSize="9" Foreground="#FFFEFEFE" FontWeight="Normal"/>
          <Label Background="{x:Null}" HorizontalAlignment="Right" Margin="0,-10,0,5" VerticalAlignment="Top" Width="180.217" Content="4 www.Codeproject.com" FontFamily="Agency FB" FontSize="20" Foreground="#FFFFC934" FontWeight="Bold" RenderTransformOrigin="0.5,0.5" Height="36.2">
            <Label.RenderTransform>
              <TransformGroup>
                <ScaleTransform ScaleX="-1" ScaleY="1"/>
                <SkewTransform AngleX="0" AngleY="0"/>
                <RotateTransform Angle="0"/>
                <TranslateTransform X="0" Y="0"/>
              </TransformGroup>
            </Label.RenderTransform>
          </Label>
        </StackPanel>
      </DockPanel>
    </Border>

    <!-- Menu Area -->
    <Border x:Name="mnuBorder" VerticalAlignment="Top" DockPanel.Dock="Top" Margin="0,5,0,5">
      <Menu VerticalAlignment="Bottom" Margin="5,0,0,0" Style="{DynamicResource MainMenuStyle}" Background="#FFFFFDFD" >
        <MenuItem Header="DisConnect" x:Name="mnuDisConnect" Style="{DynamicResource MenuItemStyle}" Click="mnuDisConnect_Click" FontFamily="Agency FB" FontSize="15"/>
        <MenuItem Header="Exit" x:Name="menuExit" Style="{DynamicResource MenuItemStyle}" Click="mnuExit_Click" FontFamily="Agency FB" FontSize="15"/>
      </Menu>
    </Border>


    <!-- Main Area -->
    <Grid x:Name="MainGrid" DockPanel.Dock="Top"  Background="#FF2E2E2E"  >

      <Border x:Name="MainBorder" Margin="10,30,10,10" BorderBrush="#FFFEFEFE" BorderThickness="1,1,1,1" CornerRadius="5,5,5,5">

        <Border.Background>
          <LinearGradientBrush EndPoint="0.504,0.632" StartPoint="0.504,0.03">
            <GradientStop Color="#FFFFC934" Offset="0"/>
            <GradientStop Color="#FFFFFFFF" Offset="0.567"/>
          </LinearGradientBrush>
        </Border.Background>

        <DockPanel>
          <!-- Top Main Area -->
          <StackPanel Width="Auto" Height="235"  DockPanel.Dock="Top"  Orientation="Horizontal" VerticalAlignment="Top">
            <!-- Reflected Image -->
            <StackPanel Margin="30" x:Name="photoBigContainer">
              <Border BorderBrush="White" BorderThickness="4" Width="108" Height="88">
                <Image Width="100" Height="80" Name="photoBig" Stretch="Fill" />
              </Border>
              <Border Width="108" Height="88" Opacity="0.2" BorderBrush="White" BorderThickness="4">
                <Border.OpacityMask>
                  <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                    <LinearGradientBrush.GradientStops>
                      <GradientStop Offset="0" Color="Black"/>
                      <GradientStop Offset=".6" Color="Transparent"/>
                    </LinearGradientBrush.GradientStops>
                  </LinearGradientBrush>
                </Border.OpacityMask>
                <Border.Background>
                  <VisualBrush Visual="{Binding ElementName=photoBig}">
                    <VisualBrush.Transform>
                      <ScaleTransform ScaleX="1" ScaleY="-1" CenterX="50" CenterY="40" />
                    </VisualBrush.Transform>
                  </VisualBrush>
                </Border.Background>
              </Border>
            </StackPanel>
            <StackPanel Orientation="Vertical" Margin="0,0,0,0">

              <Label Width="Auto" Height="55" FontFamily="Agency FB" FontSize="35" Foreground="#FFFFFFFF" x:Name="txtPerson" Margin="0,25,0,0" FontWeight="Bold">
                <Label.BitmapEffect>
                  <DropShadowBitmapEffect Color="#FF0C0C0C" Noise="0.1" ShadowDepth="10" Softness="0.4"/>
                </Label.BitmapEffect>
              </Label>
              <Label x:Name="lblInstructions"  Width="548" Height="91.21" FontWeight="Normal" FontFamily="Agency FB" FontSize="15" Foreground="#FF2E2E2E" Background="{x:Null}" BorderBrush="{x:Null}" FontStyle="Italic" />


            </StackPanel>
          </StackPanel>


          <DockPanel DockPanel.Dock="Bottom" x:Name="bottomDocker">

		      <Label DockPanel.Dock="Top" Margin="10,0,0,0" Content="Pick a fellow Chatter to talk to" FontFamily="Agency FB" FontSize="15" Foreground="#FF000000" x:Name="lblListHeader" FontStyle="Italic" FontWeight="Normal" Height="30">
		      	<Label.BitmapEffect>
		      		<OuterGlowBitmapEffect GlowColor="#FFFFC936" GlowSize="8"/>
		      	</Label.BitmapEffect>
		      </Label>
          <ListView DockPanel.Dock="Bottom" Margin="0,-10,0,0"  VerticalAlignment="Bottom" x:Name="lstChatters" SelectionMode="Single" ItemContainerStyle="{StaticResource ListViewContainer}" Background="{x:Null}" BorderBrush="#FFFFFBFB" Foreground="#FFB5B5B5" Opacity="1" BorderThickness="2,2,2,2" HorizontalAlignment="Stretch" Width="Auto" Height="Auto">
            <ListView.View>
              <GridView>
                <GridView.ColumnHeaderContainerStyle>
                  <Style TargetType="GridViewColumnHeader">
                    <Setter Property="Visibility" Value="Hidden" />
                    <Setter Property="Height" Value="0" />
                  </Style>
                </GridView.ColumnHeaderContainerStyle>
                <GridViewColumn Header="Image" HeaderTemplate="{StaticResource noTextHeaderTemplate}" Width="100" CellTemplate="{StaticResource imageCellTemplate}"/>
                <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="First Name" HeaderTemplate="{StaticResource textCellTemplate}" Width="100"/>
              </GridView>
            </ListView.View>
          </ListView>

            </DockPanel>
        </DockPanel>

      </Border>

      <local:SignInControl x:Name="SignInControl" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Hidden"/>
      <local:ChatControl x:Name="ChatControl" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Hidden" RenderTransformOrigin="0.5,0.5">
      	<local:ChatControl.RenderTransform>
      		<TransformGroup>
      			<ScaleTransform ScaleX="1" ScaleY="1"/>
      			<SkewTransform AngleX="0" AngleY="0"/>
      			<RotateTransform Angle="0"/>
      			<TranslateTransform X="0" Y="0"/>
      		</TransformGroup>
      	</local:ChatControl.RenderTransform>
      </local:ChatControl>


    </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 Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions