How to Display Different Window Styles in WPF Using F#

Today I am going to explain how you can display different window styles in WPF using F#. If you want to show a different window style firstly you will create a MenuItem Object to change the window style than you will add the items (Window Styles) to the menu. Then you will add this style menu to a Dock Panel and lastly add a TextBlock to the Dock panel to show the Text in the Window. where you can set the Title, FontSize and Alignment properties for the Text.

A MenuItem object will be created by below code.

// This will Create the MenuItem objects to change WindowStyle
let stylItm = new MenuItem(Header="_WindowStyle");

The below given code will add the Items to the Menu.

// This will Add window styles menu items
[("_Window Without Border", WindowStyle.None);
 ("_Window with Single Border",WindowStyle.SingleBorderWindow);
 ("_Window with 3D Border",WindowStyle.ThreeDBorderWindow);
 ("_Window with Tool",WindowStyle.ToolWindow)]
 |> Seq.iter (fun (str,style) ->
      let cmpnt = new MenuItem(Header=str,
                              Tag=style,
                              IsChecked= (style=wndw.WindowStyle))
      if cmpnt.IsChecked then itemChecked <- cmpnt
      cmpnt.Click.Add( fun _ ->
         itemChecked.IsChecked <- false
         cmpnt.IsChecked <- true
         wndw.WindowStyle <-(cmpnt.Tag :?> WindowStyle)
         itemChecked <- cmpnt)
        
      stylItm.Items.Add(cmpnt) |> ignore)

Then you will add this style Menu to the Dock Panel with the below code.

// This will Add Style menu to dock panel
let nwmnu = new Menu()
nwmnu.Items.Add(stylItm)|>ignore
dck.Children.Add(nwmnu)|>ignore
DockPanel.SetDock(nwmnu, Dock.Top)

Lastly a TextBlock will be added to the Dock Panel with the below code.

//This will Add TextBlock to DockPanel
dck.Children.Add
   (new TextBlock(Text=wndw.Title,
                  FontSize=25.0,
                  TextAlignment = TextAlignment.Center)
   ) |> ignore

Steps For Window style

Step 1: Firstly Open a new project in F# using Visual Studio 2010. Select F# WPF application template and give name to it like below image.

New Project Dialog Box

Step 2: Now add the below define references to the project by right clicking on the project in Solution Explorer.

  • PresentationCore

  • PresentationFramework

  • System

  • System.Xaml

  • WindowsBase

Step 3: When you have added all these references, your Solution Explorer will look like the below image.

Solution Explorer

Step 4: Now click on Program.fs in Solution Explorer and write the below code in the Program.fs window. Your window will look like below.

Window Style code part1

Window Style code part1.1

#light
 
open System
open System.Windows
open System.Windows.Controls
open System.Windows.Input
open System.Windows.Media
 
let mutable itemChecked:MenuItem = null
 
let dck = new DockPanel()
 
let wndw = new Window(Title="Different Window Styles",
                        SizeToContent = SizeToContent.WidthAndHeight,
                        Content=dck)
 
// This will Create the MenuItem objects to change WindowStyle
let stylItm = new MenuItem(Header="_WindowStyle");
 
// This will Add window styles menu items
[("_Window Without Border", WindowStyle.None);
 ("_Window with Single Border",WindowStyle.SingleBorderWindow);
 ("_Window with 3D Border",WindowStyle.ThreeDBorderWindow);
 ("_Window with Tool",WindowStyle.ToolWindow)]
 |> Seq.iter (fun (str,style) ->
      let cmpnt = new MenuItem(Header=str,
                              Tag=style,
                              IsChecked= (style=wndw.WindowStyle))
      if cmpnt.IsChecked then itemChecked <- cmpnt
      cmpnt.Click.Add( fun _ ->
         itemChecked.IsChecked <- false
         cmpnt.IsChecked <- true
         wndw.WindowStyle <-(cmpnt.Tag :?> WindowStyle)
         itemChecked <- cmpnt)
        
      stylItm.Items.Add(cmpnt) |> ignore)
     
// This will Add Style menu to dock panel
let nwmnu = new Menu()
nwmnu.Items.Add(stylItm)|>ignore
dck.Children.Add(nwmnu)|>ignore
DockPanel.SetDock(nwmnu, Dock.Top)
  
//This will Add TextBlock to DockPanel
dck.Children.Add
   (new TextBlock(Text=wndw.Title,
                  FontSize=25.0,
                  TextAlignment = TextAlignment.Center)
   ) |> ignore
 
 
#if COMPILED
[<STAThread()>]
do
    let app =  Application() in
    app.Run(wndw) |> ignore
#endif

Step 5: Now press F5 to execute the Code.

Output

Firstly you will get the default Style of Windows which is a window with single Border.

Output 1

Output2

Then you will get the window without Border,3D Border and other styles as well.

Output3

Output4

Output5

Summary

In this article I have discussed how you can display different window styles in WPF using F#.

Up Next
    Ebook Download
    View all
    Learn
    View all