Search...

Friday, July 19, 2013

How to bind DataTable to DataGrid in WPF

Download

Step 1: Create a WPF application and update the MainWindow.xaml as follows.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<Window x:Class="BindDataTable.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DataGridAndDataTable" Height="273" Width="721" Loaded="Window_Loaded">
    <Grid>
        <DataGrid Name="dataGridEmployee" AutoGenerateColumns="False" AlternatingRowBackground="#c2ccdb">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Id}" Header="Id" Width="80"/>
                <DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="200"/>
                <DataGridTextColumn Binding="{Binding Salary}" Header="Salary" Width="100"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>

</Window>

Step 2: Update the MainWindow.cs as follows.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Data;
using System.Windows;

namespace BindDataTable
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                this.dataGridEmployee.ItemsSource = this.BindEmployee().DefaultView;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private DataTable BindEmployee()
        {
            try
            {
                DataTable employee = new DataTable();
                employee.Columns.Add(new DataColumn("Id", Type.GetType("System.Int32")));
                employee.Columns.Add(new DataColumn("Name", Type.GetType("System.String")));
                employee.Columns.Add(new DataColumn("Salary", Type.GetType("System.Decimal")));

                int iterator = 0;

                while (++iterator < 101)
                {
                    DataRow row = null;
                    row = employee.NewRow();
                    row["Id"] = iterator;
                    row["Name"] = "Employee " + iterator;
                    row["Salary"] = ((9999 * iterator) * 5.6) / (iterator+1);
                    employee.Rows.Add(row);
                }
                return employee;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}

Thursday, July 11, 2013

Configure an IIS-hosted WCF service with SSL

Creating a Self-Signed Certificate

Step 1:Open Internet Information Services Manager (inetmgr.exe), and select your computer name in the left-hand tree view. On the right-hand side of the screen select Server Certificates


Step 2: In the Server Certificates window click the Create Self-Signed Certificate…. Link.


Step 3: Enter a friendly name for the self-signed certificate and click OK.





The newly created self-signed certificate details are now shown in the Server Certificates window.


The generated certificate is installed in the Trusted Root Certification Authorities store.

Step 4:  Expand the Sites folder and then the Default Web Site folder in the tree view on the left-hand side of the screen, Click the Bindings…. Link in the Actions section in the upper right hand portion of the window.



Step 5: Configure Virtual Directory for SSL 

Select the virtual directory that contains your WCF secure service, Select SSL Settings in the IIS section.


In the SSL Settings pane, select the Require SSL checkbox and click the Apply link in the Actions section on the right hand side of the screen.



Step 6: Configure Virtual Directory for Authentication  

In the SSL Setting pane, select Authentication and click the Open Feature link



Select Windows Authentication and select Enable



Step 7: Configure WCF Service for HTTP Transport Security

In the WCF service’s web.config configure the HTTP binding to use transport security as shown in the following XML.

    <bindings>
      <basicHttpBinding>
        <binding name="ssl">
          <security mode="Transport">
            <transport clientCredentialType="Windows"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>

Specify your service and service endpoint as shown in the following XML.

<services>
      <service name="WCFWithSSL.Service1">
        <host>
          <baseAddresses>
            <add baseAddress="https://localhost/SSL"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="ssl" contract="WCFWithSSL.IService1"/>
      </service>
    </services>

The following is a complete example of a web.config file for a WCF service using HTTP transport security

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WCFWithSSL.Service1">
        <host>
          <baseAddresses>
            <add baseAddress="https://localhost/SSL"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="ssl" contract="WCFWithSSL.IService1"/>
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="ssl">
          <security mode="Transport">
            <transport clientCredentialType="Windows"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata  httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>

Step 7: Brows your application from SSL

Tuesday, July 9, 2013

Decorate label in WPF

Step 1: Create a WPF application and add new item Resource Directory(WPF) and named it Style.xaml



Step 2: Update the MainWindow.xaml as follows.

<Window x:Class="LabelControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="274" Width="470">

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Style.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
 
    <WrapPanel>
        <Label Content="Hello World!" Style="{StaticResource roundLabel}"/>
        <Label Content="Hello World!" Style="{StaticResource styleTextLabel}"/>
        <Label Content="Hello World!" Style="{StaticResource styleBorderTextLabel}"/>
        <Label Content="Hello World!" Style="{StaticResource styleImageBrushLabel}"/>
    </WrapPanel>
</Window>

*********************************************************************

Step 3: Update the Style.xaml as follows.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="roundLabel" TargetType="{x:Type Label}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Label}">
                    <Border BorderBrush="#BFE3FE" BorderThickness="1" CornerRadius="5" Padding="3">
                        <Border.Background>
                            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                <GradientStop Color="#FFc2ccdb" Offset="1"/>
                                <GradientStop Color="#FFF1EDED" Offset="0"/>
                            </LinearGradientBrush>
                        </Border.Background>
                        <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Margin" Value="2"/>
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="FontWeight" Value="DemiBold"/>
        <Setter Property="Width" Value="200"/>
        <Setter Property="Height" Value="70"/>
        <Setter Property="FontSize" Value="22"/>
    </Style>

    <LinearGradientBrush x:Key="ForeGroundBrush" StartPoint="0,0" EndPoint="0,1">
        <LinearGradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="Blue" Offset="0.1" />
                    <GradientStop Color="Orange" Offset="0.25" />
                    <GradientStop Color="Green" Offset="0.75" />
            </GradientStopCollection>
        </LinearGradientBrush.GradientStops>
    </LinearGradientBrush>

    <Style x:Key="styleTextLabel" TargetType="{x:Type Label}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Label}">
                        <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Margin" Value="2"/>
        <Setter Property="Foreground" Value="{StaticResource ForeGroundBrush}"/>
        <Setter Property="FontWeight" Value="DemiBold"/>
        <Setter Property="Width" Value="200"/>
        <Setter Property="Height" Value="70"/>
        <Setter Property="FontSize" Value="22"/>
    </Style>

    <Style x:Key="styleBorderTextLabel" TargetType="{x:Type Label}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Label}">
                    <Border BorderBrush="{StaticResource ForeGroundBrush}" BorderThickness="2" CornerRadius="5" Padding="3">
                        <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Margin" Value="2"/>
        <Setter Property="Foreground" Value="{StaticResource ForeGroundBrush}"/>
        <Setter Property="FontWeight" Value="DemiBold"/>
        <Setter Property="Width" Value="200"/>
        <Setter Property="Height" Value="70"/>
        <Setter Property="FontSize" Value="22"/>
    </Style>

    <Style x:Key="styleImageBrushLabel" TargetType="{x:Type Label}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Label}">
                    <Border BorderBrush="#BFE3FE" BorderThickness="2" CornerRadius="5" Padding="3">
                        <Border.Background>
                            <ImageBrush ImageSource="Images/Tulips.jpg"/>
                        </Border.Background>
                        <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Margin" Value="2"/>
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="FontWeight" Value="DemiBold"/>
        <Setter Property="Width" Value="200"/>
        <Setter Property="Height" Value="70"/>
        <Setter Property="FontSize" Value="22"/>
    </Style>
    
</ResourceDictionary>

*********************************************************************
If every thing goes you will get following output.