Click here to Skip to main content
15,886,664 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
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
    >

  
  <ControlTemplate x:Key="SignInButtonTemplate" TargetType="{x:Type Button}">
    <Grid>
      <Border x:Name="border"   Background="White" BorderBrush="#FFFFC934" BorderThickness="2" CornerRadius="5,5,5,5" Width="Auto" Height="Auto">
      </Border>
      <ContentControl Content="{TemplateBinding Content}" Width="Auto" Height="Auto" VerticalAlignment="Center" HorizontalAlignment="Center" FontFamily="Agency FB" FontSize="15"/>
    </Grid>
    <ControlTemplate.Triggers>
      <Trigger Property="IsMouseOver" Value="True">
        <Setter TargetName="border" Property="BorderBrush" Value="Red"/>
        <Setter TargetName="border" Property="Background" Value="#FFFFC934"/>
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>


  <Style x:Key="ListViewContainer" TargetType="{x:Type ListViewItem}">
    <Setter Property="FontWeight" Value="Normal"/>
    <Setter Property="Foreground" Value="#FF000000"/>
    <Setter Property="FontFamily" Value="Agency FB"/>
    <Setter Property="FontSize" Value="15"/>
    <Style.Triggers>
      <Trigger Property="IsMouseOver" Value="true">
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="Background">
          <Setter.Value>
            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
              <LinearGradientBrush.GradientStops>
                <GradientStop Color="#D88" Offset="0"/>
                <GradientStop Color="#D31" Offset="1"/>
              </LinearGradientBrush.GradientStops>
            </LinearGradientBrush>
          </Setter.Value>
        </Setter>
        <Setter Property="Cursor" Value="Hand"/>
      </Trigger>
      <MultiTrigger>
        <MultiTrigger.Conditions>
          <Condition Property="IsSelected" Value="true" />
          <Condition Property="Selector.IsSelectionActive" Value="true" />
        </MultiTrigger.Conditions>
        <Setter Property="Background">
          <Setter.Value>
            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
              <LinearGradientBrush.GradientStops>
                <GradientStop Color="#0E4791" Offset="0"/>
                <GradientStop Color="#468DE2" Offset="1"/>
              </LinearGradientBrush.GradientStops>
            </LinearGradientBrush>
          </Setter.Value>
        </Setter>
        <Setter Property="Foreground" Value="Black" />
      </MultiTrigger>
    </Style.Triggers>
  </Style>  
  
  

  <!-- Gridview Templates  -->
  <DataTemplate x:Key="noTextHeaderTemplate"/>

  <DataTemplate x:Key="textCellTemplate">
    <TextBlock Margin="10,0,0,0" Text="{Binding}" VerticalAlignment="Center"/>
  </DataTemplate>

  <DataTemplate x:Key="imageCellTemplate">
    <Border CornerRadius="2,2,2,2" Width="40" Height="40" Background="#FFFFC934" BorderBrush="#FF000000" Margin="3,3,3,3">
      <Image HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" Height="Auto" Source="{Binding Path=ImageURL}" Stretch="Fill" Margin="2,2,2,2"/>
    </Border>
  </DataTemplate>




  <!-- Menu on main window -->
  <LinearGradientBrush x:Key="MenuBackground" EndPoint="0,1" StartPoint="0,0">
    <GradientStop Color="#F6F6F6" Offset="0.25"/>
    <GradientStop Color="#EAE8E8" Offset="0.25"/>
    <GradientStop Color="#DCD9D9" Offset="0.8"/>
    <GradientStop Color="White" Offset="1"/>
  </LinearGradientBrush>
  <Style x:Key="MainMenuStyle" TargetType="{x:Type Menu}">
    <Setter Property="Background" Value="{StaticResource MenuBackground}"/>
    <Setter Property="FontFamily" Value="{DynamicResource {x:Static SystemFonts.MenuFontFamilyKey}}"/>
    <Setter Property="FontSize" Value="{DynamicResource {x:Static SystemFonts.MenuFontSizeKey}}"/>
    <Setter Property="FontStyle" Value="{DynamicResource {x:Static SystemFonts.MenuFontStyleKey}}"/>
    <Setter Property="FontWeight" Value="{DynamicResource {x:Static SystemFonts.MenuFontWeightKey}}"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type Menu}">
          <Border Width="Auto" Height="Auto" Background="#FFF8F8F8">
            <ItemsPresenter Width="Auto" Height="Auto"/>
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
  <Geometry x:Key="Checkmark">M 0,5.1 L 1.7,5.2 L 3.4,7.1 L 8,0.4 L 9.2,0 L 3.3,10.8 Z</Geometry>
  <LinearGradientBrush x:Key="MenuItemSelectionFill" EndPoint="0,1" StartPoint="0,0">
    <GradientStop Color="#34C5EBFF" Offset="0"/>
    <GradientStop Color="#3481D8FF" Offset="1"/>
  </LinearGradientBrush>
  <ControlTemplate x:Key="{ComponentResourceKey ResourceId=SubmenuItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}">
    <Grid SnapsToDevicePixels="true">
      <Rectangle Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="1" RadiusX="2" RadiusY="2" x:Name="Bg"/>
      <Rectangle RadiusX="2" RadiusY="2" Margin="1" x:Name="InnerBorder"/>
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition MinWidth="24" Width="Auto" SharedSizeGroup="MenuItemIconColumnGroup"/>
          <ColumnDefinition Width="4"/>
          <ColumnDefinition Width="*"/>
          <ColumnDefinition Width="37"/>
          <ColumnDefinition Width="Auto" SharedSizeGroup="MenuItemIGTColumnGroup"/>
          <ColumnDefinition Width="17"/>
        </Grid.ColumnDefinitions>
        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="1" x:Name="Icon" VerticalAlignment="Center" ContentSource="Icon"/>
        <Border Visibility="Hidden" Margin="1" x:Name="GlyphPanel" Width="22" Height="22" Background="#E6EFF4" BorderBrush="#CDD3E6" BorderThickness="1" CornerRadius="3">
          <Path Fill="#0C12A1" FlowDirection="LeftToRight" x:Name="Glyph" Width="9" Height="11" Data="{StaticResource Checkmark}"/>
        </Border>
        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="{TemplateBinding Padding}" Grid.Column="2" ContentSource="Header" RecognizesAccessKey="True"/>
        <TextBlock Margin="{TemplateBinding Padding}" Text="{TemplateBinding InputGestureText}" Grid.Column="4"/>
      </Grid>
    </Grid>
    <ControlTemplate.Triggers>
      <Trigger Property="Icon" Value="{x:Null}">
        <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
      </Trigger>
      <Trigger Property="IsChecked" Value="true">
        <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
        <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
      </Trigger>
      <Trigger Property="IsHighlighted" Value="true">
        <Setter Property="Fill" TargetName="Bg" Value="{StaticResource MenuItemSelectionFill}"/>
        <Setter Property="Stroke" TargetName="Bg" Value="#8071CBF1"/>
        <Setter Property="Stroke" TargetName="InnerBorder" Value="#40FFFFFF"/>
      </Trigger>
      <Trigger Property="IsEnabled" Value="false">
        <Setter Property="Foreground" Value="#FF9A9A9A"/>
        <Setter Property="Background" TargetName="GlyphPanel" Value="#EEE9E9"/>
        <Setter Property="BorderBrush" TargetName="GlyphPanel" Value="#DBD6D6"/>
        <Setter Property="Fill" TargetName="Glyph" Value="#848589"/>
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>
  <MenuScrollingVisibilityConverter x:Key="MenuScrollingVisibilityConverter"/>
  <Style x:Key="MenuScrollButton" BasedOn="{x:Null}" TargetType="{x:Type RepeatButton}">
    <Setter Property="ClickMode" Value="Hover"/>
    <Setter Property="MinWidth" Value="0"/>
    <Setter Property="MinHeight" Value="0"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type RepeatButton}">
          <DockPanel SnapsToDevicePixels="true" Background="Transparent">
            <Rectangle Fill="Transparent" x:Name="R1" Width="1" DockPanel.Dock="Right"/>
            <Rectangle Fill="Transparent" x:Name="B1" Height="1" DockPanel.Dock="Bottom"/>
            <Rectangle Fill="Transparent" x:Name="L1" Width="1" DockPanel.Dock="Left"/>
            <Rectangle Fill="Transparent" x:Name="T1" Height="1" DockPanel.Dock="Top"/>
            <ContentPresenter HorizontalAlignment="Center" Margin="2,2,2,2" x:Name="ContentContainer" VerticalAlignment="Center"/>
          </DockPanel>
          <ControlTemplate.Triggers>
            <Trigger Property="IsPressed" Value="true">
              <Setter Property="Fill" TargetName="R1" Value="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}"/>
              <Setter Property="Fill" TargetName="B1" Value="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}"/>
              <Setter Property="Fill" TargetName="L1" Value="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}"/>
              <Setter Property="Fill" TargetName="T1" Value="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}"/>
              <Setter Property="Margin" TargetName="ContentContainer" Value="3,3,1,1"/>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
  <Geometry x:Key="UpArrow">M 0,4 L 3.5,0 L 7,4 Z</Geometry>
  <Geometry x:Key="DownArrow">M 0,0 L 3.5,4 L 7,0 Z</Geometry>
  <Style x:Key="{ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}" BasedOn="{x:Null}" TargetType="{x:Type ScrollViewer}">
    <Setter Property="HorizontalScrollBarVisibility" Value="Hidden"/>
    <Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type ScrollViewer}">
          <Grid SnapsToDevicePixels="true">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
              <RowDefinition Height="Auto"/>
              <RowDefinition Height="*"/>
              <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Border Grid.Column="0" Grid.Row="1">
              <ScrollContentPresenter Margin="{TemplateBinding Padding}"/>
            </Border>
            <RepeatButton Focusable="false" Style="{StaticResource MenuScrollButton}" Grid.Column="0" Grid.Row="0" Command="{x:Static ScrollBar.LineUpCommand}" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}">
              <RepeatButton.Visibility>
                <MultiBinding FallbackValue="Visibility.Collapsed" Converter="{StaticResource MenuScrollingVisibilityConverter}" ConverterParameter="0">
                  <Binding Path="ComputedVerticalScrollBarVisibility" RelativeSource="{RelativeSource TemplatedParent}"/>
                  <Binding Path="VerticalOffset" RelativeSource="{RelativeSource TemplatedParent}"/>
                  <Binding Path="ExtentHeight" RelativeSource="{RelativeSource TemplatedParent}"/>
                  <Binding Path="ViewportHeight" RelativeSource="{RelativeSource TemplatedParent}"/>
                </MultiBinding>
              </RepeatButton.Visibility>
              <Path Fill="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}" Data="{StaticResource UpArrow}"/>
            </RepeatButton>
            <RepeatButton Focusable="false" Style="{StaticResource MenuScrollButton}" Grid.Column="0" Grid.Row="2" Command="{x:Static ScrollBar.LineDownCommand}" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}">
              <RepeatButton.Visibility>
                <MultiBinding FallbackValue="Visibility.Collapsed" Converter="{StaticResource MenuScrollingVisibilityConverter}" ConverterParameter="100">
                  <Binding Path="ComputedVerticalScrollBarVisibility" RelativeSource="{RelativeSource TemplatedParent}"/>
                  <Binding Path="VerticalOffset" RelativeSource="{RelativeSource TemplatedParent}"/>
                  <Binding Path="ExtentHeight" RelativeSource="{RelativeSource TemplatedParent}"/>
                  <Binding Path="ViewportHeight" RelativeSource="{RelativeSource TemplatedParent}"/>
                </MultiBinding>
              </RepeatButton.Visibility>
              <Path Fill="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}" Data="{StaticResource DownArrow}"/>
            </RepeatButton>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
  <ControlTemplate x:Key="{ComponentResourceKey ResourceId=SubmenuContent, TypeInTargetAssembly={x:Type FrameworkElement}}" TargetType="{x:Type ContentControl}">
    <Border Background="#F5F5F5" BorderBrush="#FF959595" BorderThickness="1">
      <Grid>
        <Rectangle Fill="#F1F1F1" RadiusX="2" RadiusY="2" HorizontalAlignment="Left" Margin="2" Width="28"/>
        <Rectangle Fill="#E2E3E3" HorizontalAlignment="Left" Margin="30,2,0,2" Width="1"/>
        <Rectangle Fill="White" HorizontalAlignment="Left" Margin="31,2,0,2" Width="1"/>
        <ContentPresenter Margin="1,0" Grid.ColumnSpan="2"/>
      </Grid>
    </Border>
  </ControlTemplate>
  <LinearGradientBrush x:Key="MenuItemPressedFill" EndPoint="0,1" StartPoint="0,0">
    <GradientStop Color="#28717070" Offset="0"/>
    <GradientStop Color="#50717070" Offset="0.75"/>
    <GradientStop Color="#90717070" Offset="1"/>
  </LinearGradientBrush>
  <ControlTemplate x:Key="{ComponentResourceKey ResourceId=TopLevelHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}">
    <Grid SnapsToDevicePixels="true">
      <Rectangle RadiusX="2" RadiusY="2" x:Name="OuterBorder"/>
      <Rectangle Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="1" RadiusX="1" RadiusY="1" Margin="1" x:Name="Bg"/>
      <Rectangle Margin="2" x:Name="InnerBorder"/>
      <DockPanel>
        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="4,0,6,0" x:Name="Icon" VerticalAlignment="Center" ContentSource="Icon"/>
        <Path Visibility="Collapsed" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Margin="7,0,0,0" x:Name="GlyphPanel" VerticalAlignment="Center" Data="{StaticResource Checkmark}"/>
        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="{TemplateBinding Padding}" ContentSource="Header" RecognizesAccessKey="True"/>
      </DockPanel>
      <Popup Focusable="false" AllowsTransparency="true" HorizontalOffset="1" IsOpen="{Binding Path=IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Bottom" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" VerticalOffset="-1" x:Name="PART_Popup">
        <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent">
          <ContentControl x:Name="SubMenuBorder" IsTabStop="false" Template="{DynamicResource {ComponentResourceKey ResourceId=SubmenuContent, TypeInTargetAssembly={x:Type FrameworkElement}}}">
            <ScrollViewer Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}" CanContentScroll="true">
              <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="2" Grid.IsSharedSizeScope="true" KeyboardNavigation.DirectionalNavigation="Cycle" KeyboardNavigation.TabNavigation="Cycle"/>
            </ScrollViewer>
          </ContentControl>
        </Microsoft_Windows_Themes:SystemDropShadowChrome>
      </Popup>
    </Grid>
    <ControlTemplate.Triggers>
      <Trigger Property="IsSuspendingPopupAnimation" Value="true">
        <Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/>
      </Trigger>
      <Trigger Property="Icon" Value="{x:Null}">
        <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
      </Trigger>
      <Trigger Property="IsChecked" Value="true">
        <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
        <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
      </Trigger>
      <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
        <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/>
        <Setter Property="Color" TargetName="Shdw" Value="#71000000"/>
      </Trigger>
      <Trigger Property="IsHighlighted" Value="true">
        <Setter Property="Stroke" TargetName="Bg" Value="#90717070"/>
        <Setter Property="Stroke" TargetName="OuterBorder" Value="#50FFFFFF"/>
        <Setter Property="Stroke" TargetName="InnerBorder" Value="#50FFFFFF"/>
      </Trigger>
      <Trigger Property="IsKeyboardFocused" Value="true">
        <Setter Property="Stroke" TargetName="Bg" Value="#E0717070"/>
        <Setter Property="Fill" TargetName="Bg" Value="{StaticResource MenuItemPressedFill}"/>
        <Setter Property="Stroke" TargetName="InnerBorder" Value="#50747272"/>
      </Trigger>
      <Trigger Property="IsSubmenuOpen" Value="true">
        <Setter Property="Stroke" TargetName="Bg" Value="#E0717070"/>
        <Setter Property="Fill" TargetName="Bg" Value="{StaticResource MenuItemPressedFill}"/>
        <Setter Property="Stroke" TargetName="InnerBorder" Value="#50747272"/>
      </Trigger>
      <Trigger Property="IsEnabled" Value="false">
        <Setter Property="Foreground" Value="#FF9A9A9A"/>
        <Setter Property="Fill" TargetName="GlyphPanel" Value="#848589"/>
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>
  <ControlTemplate x:Key="{ComponentResourceKey ResourceId=TopLevelItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}">
    <ControlTemplate.Resources>
      <Storyboard x:Key="OnMouseEnter1">
        <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="grid" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
          <SplineColorKeyFrame KeyTime="00:00:00" Value="#FFFFC936"/>
        </ColorAnimationUsingKeyFrames>
      </Storyboard>
      <Storyboard x:Key="OnMouseLeave1">
        <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="grid" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
          <SplineColorKeyFrame KeyTime="00:00:00" Value="#FFFFFFFF"/>
        </ColorAnimationUsingKeyFrames>
      </Storyboard>
    </ControlTemplate.Resources>
    <Grid SnapsToDevicePixels="true" x:Name="grid" Background="#FFFFFFFF">
      <Rectangle RadiusX="2" RadiusY="2" x:Name="OuterBorder"/>
      <Rectangle Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="1" RadiusX="1" RadiusY="1" Margin="1" x:Name="Bg"/>
      <Rectangle Margin="2" x:Name="InnerBorder"/>
      <DockPanel>
        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="4,0,6,0" x:Name="Icon" VerticalAlignment="Center" ContentSource="Icon"/>
        <Path Visibility="Collapsed" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Margin="7,0,0,0" x:Name="GlyphPanel" VerticalAlignment="Center" Data="{StaticResource Checkmark}"/>
        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="{TemplateBinding Padding}" ContentSource="Header" RecognizesAccessKey="True"/>
      </DockPanel>
    </Grid>
    <ControlTemplate.Triggers>
      <EventTrigger RoutedEvent="Mouse.MouseLeave" SourceName="grid">
        <BeginStoryboard x:Name="OnMouseLeave1_BeginStoryboard" Storyboard="{StaticResource OnMouseLeave1}"/>
      </EventTrigger>
      <EventTrigger RoutedEvent="Mouse.MouseEnter" SourceName="grid">
        <BeginStoryboard Storyboard="{StaticResource OnMouseEnter1}"/>
      </EventTrigger>
      <Trigger Property="Icon" Value="{x:Null}">
        <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
      </Trigger>
      <Trigger Property="IsChecked" Value="true">
        <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
        <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
      </Trigger>
      <Trigger Property="IsHighlighted" Value="true">
        <Setter Property="Stroke" TargetName="Bg" Value="#90717070"/>
        <Setter Property="Stroke" TargetName="OuterBorder" Value="#50FFFFFF"/>
        <Setter Property="Stroke" TargetName="InnerBorder" Value="#50FFFFFF"/>
      </Trigger>
      <Trigger Property="IsKeyboardFocused" Value="true">
        <Setter Property="Stroke" TargetName="Bg" Value="#E0717070"/>
        <Setter Property="Fill" TargetName="Bg" Value="{StaticResource MenuItemPressedFill}"/>
        <Setter Property="Stroke" TargetName="InnerBorder" Value="#50747272"/>
      </Trigger>
      <Trigger Property="IsEnabled" Value="false">
        <Setter Property="Foreground" Value="#FF9A9A9A"/>
        <Setter Property="Fill" TargetName="GlyphPanel" Value="#848589"/>
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>
  <Geometry x:Key="RightArrow">M 0,0 L 4,3.5 L 0,7 Z</Geometry>
  <ControlTemplate x:Key="{ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}">
    <Grid SnapsToDevicePixels="true">
      <Rectangle Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="1" RadiusX="2" RadiusY="2" x:Name="Bg"/>
      <Rectangle Stroke="Transparent" StrokeThickness="1" RadiusX="2" RadiusY="2" Margin="1" x:Name="InnerBorder"/>
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition MinWidth="24" Width="Auto" SharedSizeGroup="MenuItemIconColumnGroup"/>
          <ColumnDefinition Width="4"/>
          <ColumnDefinition Width="*"/>
          <ColumnDefinition Width="37"/>
          <ColumnDefinition Width="Auto" SharedSizeGroup="MenuItemIGTColumnGroup"/>
          <ColumnDefinition Width="17"/>
        </Grid.ColumnDefinitions>
        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="1" x:Name="Icon" VerticalAlignment="Center" ContentSource="Icon"/>
        <Border Visibility="Hidden" Margin="1" x:Name="GlyphPanel" Width="22" Height="22" Background="#E6EFF4" BorderBrush="#CDD3E6" BorderThickness="1" CornerRadius="3">
          <Path Fill="#0C12A1" FlowDirection="LeftToRight" x:Name="Glyph" Width="9" Height="11" Data="{StaticResource Checkmark}"/>
        </Border>
        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="{TemplateBinding Padding}" Grid.Column="2" ContentSource="Header" RecognizesAccessKey="True"/>
        <TextBlock Visibility="Collapsed" Margin="{TemplateBinding Padding}" Text="{TemplateBinding InputGestureText}" Grid.Column="4"/>
        <Path Fill="{TemplateBinding Foreground}" Margin="4,0,0,0" VerticalAlignment="Center" Grid.Column="5" Data="{StaticResource RightArrow}"/>
      </Grid>
      <Popup Focusable="false" AllowsTransparency="true" HorizontalOffset="-2" IsOpen="{Binding Path=IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Right" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" VerticalOffset="-3" x:Name="PART_Popup">
        <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent">
          <ContentControl x:Name="SubMenuBorder" IsTabStop="false" Template="{DynamicResource {ComponentResourceKey ResourceId=SubmenuContent, TypeInTargetAssembly={x:Type FrameworkElement}}}">
            <ScrollViewer Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}" CanContentScroll="true">
              <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="2" Grid.IsSharedSizeScope="true" KeyboardNavigation.DirectionalNavigation="Cycle" KeyboardNavigation.TabNavigation="Cycle"/>
            </ScrollViewer>
          </ContentControl>
        </Microsoft_Windows_Themes:SystemDropShadowChrome>
      </Popup>
    </Grid>
    <ControlTemplate.Triggers>
      <Trigger Property="IsSuspendingPopupAnimation" Value="true">
        <Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/>
      </Trigger>
      <Trigger Property="IsHighlighted" Value="true">
        <Setter Property="Stroke" TargetName="InnerBorder" Value="#D1DBF4FF"/>
      </Trigger>
      <Trigger Property="Icon" Value="{x:Null}">
        <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
      </Trigger>
      <Trigger Property="IsChecked" Value="true">
        <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
        <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
      </Trigger>
      <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
        <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/>
        <Setter Property="Color" TargetName="Shdw" Value="#71000000"/>
      </Trigger>
      <Trigger Property="IsHighlighted" Value="true">
        <Setter Property="Fill" TargetName="Bg" Value="{StaticResource MenuItemSelectionFill}"/>
        <Setter Property="Stroke" TargetName="Bg" Value="#8571CBF1"/>
      </Trigger>
      <Trigger Property="IsEnabled" Value="false">
        <Setter Property="Foreground" Value="#FF9A9A9A"/>
        <Setter Property="Background" TargetName="GlyphPanel" Value="#EEE9E9"/>
        <Setter Property="BorderBrush" TargetName="GlyphPanel" Value="#DBD6D6"/>
        <Setter Property="Fill" TargetName="Glyph" Value="#848589"/>
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>

  <!-- MenuItem on main window -->
  <Style x:Key="MenuItemStyle" TargetType="{x:Type MenuItem}">
    <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
    <Style.Triggers>
      <Trigger Property="Role" Value="TopLevelHeader">
        <Setter Property="Padding" Value="7,2,8,3"/>
        <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
      </Trigger>
      <Trigger Property="Role" Value="TopLevelItem">
        <Setter Property="Padding" Value="7,2,8,3"/>
        <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
      </Trigger>
      <Trigger Property="Role" Value="SubmenuHeader">
        <Setter Property="Padding" Value="2,3,2,3"/>
        <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
      </Trigger>
      <Trigger Property="Role" Value="SubmenuItem">
        <Setter Property="Padding" Value="2,3,2,3"/>
      </Trigger>
    </Style.Triggers>
  </Style>

  
  
</ResourceDictionary>

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