Today I am discussing that how
can we get the Sorted list of System Parameters. We need a GridView, ListView
and TextBlock control for implementation of Sorted SytemParameters. Firstly we
will define two GridView column and then we create a DataTemplate for the second
Column and display the List of the SystemParameters.
You will create two
GridView column by the below code block.
// Create two GridView columns.
new
GridViewColumn(Header="Property Name",
Width=200.0,
DisplayMemberBinding = new
Binding("Name"))
|> grvlu.Columns.Add
Then you will create a
DataTemplate for the second column by the below code block.
// Create DataTemplate for second column
let
template = new DataTemplate(typeof<string>)
let
factoryTextBlock = new
FrameworkElementFactory(typeof<TextBlock>)
(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Right)
|> factoryTextBlock.SetValue
(TextBlock.TextProperty,new Binding("Value"))
|> factoryTextBlock.SetBinding
template.VisualTree <- factoryTextBlock
new
GridViewColumn(Header="Value",
Width=200.0,
CellTemplate=template,
DisplayMemberBinding = new
Binding("Value"))
|> grvlu.Columns.Add
Lastly You will sort the List
of Parameters. The Below code is for sorting the Parameters.
let srtlst =
new SortedList<string,ParaSys>()
typeof<SystemParameters>.GetProperties() |> Seq.iter (fun
prop ->
if (prop.PropertyType <> typeof<ResourceKey>)
then
(prop.Name,
new ParaSys(Name=prop.Name,
Value = prop.GetValue(null,null)))
|> srtlst.Add
)
vlst.ItemsSource <- srtlst.Values
Steps For sorted SystemParameters
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.
Step 2:
Now add the below define references to the project by right clicking on project
in solution explorer.
PresentationCore
PresentationFramework
System
System.Xaml
WindowsBase
Step 3:
When you will add all these refrences your Solution Explorer will look like
below image.
Step 4:
Now click on Program.fs file in solution explorer and write below code in
Program.fs window, your window will look like below image.
#light
open
System
open
System.Collections.Generic
open
System.ComponentModel
open
System.Reflection
open
System.Windows
open
System.Windows.Controls
open
System.Windows.Data
open
System.Windows.Input
open
System.Windows.Media
type
ParaSys() = class
let
mutable nStr=""
let
mutable ovl = null
member this.Name
with get() = nStr
and set (value:string) = nStr <-
value
member this.Value
with get() = ovl
and set (value:obj) = ovl <- value
override this.ToString() =
this.Name + "=" +
this.Value.ToString()
end
let
grvlu = new GridView()
let
vlst = new ListView(View=grvlu)
//
Create two GridView columns.
new
GridViewColumn(Header="Property Name",
Width=200.0,
DisplayMemberBinding = new
Binding("Name"))
|> grvlu.Columns.Add
//
Create DataTemplate for second column
let
template = new DataTemplate(typeof<string>)
let
factoryTextBlock = new
FrameworkElementFactory(typeof<TextBlock>)
(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Right)
|> factoryTextBlock.SetValue
(TextBlock.TextProperty,new Binding("Value"))
|> factoryTextBlock.SetBinding
template.VisualTree <- factoryTextBlock
new
GridViewColumn(Header="Value",
Width=200.0,
CellTemplate=template,
DisplayMemberBinding = new
Binding("Value"))
|> grvlu.Columns.Add
let
srtlst = new SortedList<string,ParaSys>()
typeof<SystemParameters>.GetProperties() |> Seq.iter (fun
prop ->
if (prop.PropertyType <> typeof<ResourceKey>)
then
(prop.Name,
new ParaSys(Name=prop.Name,
Value = prop.GetValue(null,null)))
|> srtlst.Add
)
vlst.ItemsSource <- srtlst.Values
let
wndw = new Window(Title="Sorted
List of System Parameters",
Content=vlst)
#if
COMPILED
[<STAThread()>]
do
let app = Application()
in
app.Run(wndw) |> ignore
#endif
Step 5:
Now press F5 to execute the Code.
Output
Summary
In this
article I have Discussed that how to get the Sorted List of SystemParameters in
WPF using F#.