Friday, September 5, 2008

Using Property Grid in WPF




1)Create New Application and Select Window Application(WPF) and Enter The Name PorpertyandToolbox

2)Right Click in the Solution Explorer Add-->NewItem and Select Windows(WPF) and Enter the Name property.xaml

3)Right Click in the Solution Explorer Add Reference and select the WindowsFormsIntegration dll

4)Copy and Paste The code


property.xaml
**************
Window x:Class="PorpertyandToolbox.property"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="PorpertyGrid in Windows Presentation Foundation By S.S.Sivaprasad" Height="435" Width="666"
Loaded="Form1_Load">

Grid Background="#FFFFFFFF">

Grid.RowDefinitions>

RowDefinition Height="0.909308419733394*" />

RowDefinition Height="0.090691580266606*" />

/Grid.RowDefinitions>

WindowsFormsHost Grid.RowSpan="2" HorizontalAlignment="Left" Margin="2,78,0,3" MinHeight="50" MinWidth="50" Width="200">

wf:PropertyGrid x:Name="propertyGrid1" BackColor="">

/wf:PropertyGrid>

/WindowsFormsHost>

Label Margin="206.37,154.723333333333,11,176.541984732825" Name="label1" AllowDrop="True">Label
/Label>

StackPanel Height="76" Margin="2,1,2,0" MinHeight="50" MinWidth="50" Name="stackPanel1" VerticalAlignment="Top" Background="#FF808080" />

Label Height="32.2766666666667" Margin="234.37,11.7233333333333,224,0" Name="label2" VerticalAlignment="Top" FontSize="20" AllowDrop="False">PropertyGrid in WPF
/Label>

Label Margin="203.430126582278,0.181348600508954,-0.0601265822784853,3" Name="label3" Background="#FF808080" Grid.Row="1">
/Label>

Button Margin="0,5.4580152671756,220,8" Name="button1" HorizontalAlignment="Right" Grid.Row="1" Width="75" Click="onclick">ClickMe
/Button>

/Grid>

/Window>



property.xaml.cs
****************
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace PorpertyandToolbox
{
///
/// Interaction logic for property.xaml
///



public partial class property : System.Windows.Window
{

public property()
{
InitializeComponent();
}
Customer bill = new Customer();
private void Form1_Load(object sender, System.EventArgs e)
{

bill.Age = 23;
bill.Content = "SivaPrasad";
bill.Address = "Thiruvanthapuram";
bill.DateOfBirth = Convert.ToDateTime("6/26/85");
bill.SSN = "123-345-3566";
bill.Email = "Kuvanka@gmail.com";
bill.Name = "label1";
bill.BackColor = "Gray";
bill.ForeColor = "Black";
bill.FontSize = Convert.ToDouble("12");
propertyGrid1.SelectedObject = bill;

}
private void onclick(object sender, System.EventArgs e)
{
label1.Content = bill.Content;
label1.Name = bill.Name;
label1.FontSize = bill.FontSize;

string strBaCkcolor=bill.BackColor;
string strForeColor=bill.ForeColor;
double dblFontSize = bill.FontSize;
label1.FontSize = dblFontSize;


if (strBaCkcolor == "Gray")
{
label1.Background = System.Windows.Media.Brushes.Gray;
}
else if (strBaCkcolor == "Black")
{
label1.Background = System.Windows.Media.Brushes.Black;
}
else if (strBaCkcolor == "Blue")
{
label1.Background = System.Windows.Media.Brushes.Blue;
}
else if (strBaCkcolor == "Green")
{
label1.Background = System.Windows.Media.Brushes.Green;
}
else if (strBaCkcolor == "Red")
{
label1.Background = System.Windows.Media.Brushes.Red;
}

else if (strBaCkcolor == "White")
{
label1.Background = System.Windows.Media.Brushes.White;
}

else
{
label1.Background = System.Windows.Media.Brushes.Gray;
}




if (strForeColor == "Black")
{
label1.Foreground = System.Windows.Media.Brushes.Black;
}
else if (strForeColor == "Blue")
{
label1.Foreground = System.Windows.Media.Brushes.Blue;
}
else if (strForeColor == "Green")
{
label1.Foreground = System.Windows.Media.Brushes.Green;
}
else if (strForeColor == "Red")
{
label1.Foreground = System.Windows.Media.Brushes.Red;
}
else if (strForeColor == "White")
{
label1.Foreground = System.Windows.Media.Brushes.White;
}


else
{
label1.Foreground = System.Windows.Media.Brushes.Black;
}
}


}
}


5)Right Click in the Solution Explorer Add-->Class and Enter The Name Customer.cs


Customer.cs
************
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace PorpertyandToolbox
{
[DefaultPropertyAttribute("Name")]
public class Customer
{
private string _name;
private string _content;
private int _age;
private DateTime _dateOfBirth;
private string _SSN;
private string _address;
private string _email;
private bool _frequentBuyer;
private string _forecolor;
private string _backcolor;
private double _fontsize;


[CategoryAttribute("ID Settings"), DescriptionAttribute("Name of the Label")]
public string Name
{
get
{
return _name;
}

set
{
_name = value;
}
}

[CategoryAttribute("ID Settings"), DescriptionAttribute("Content of the Label")]
public string Content
{
get
{
return _content;
}

set
{
_content = value;
}
}


[CategoryAttribute("ID Settings"), DescriptionAttribute("Social Security Number of the customer")]
public string SSN
{
get
{
return _SSN;
}

set
{
_SSN = value;
}
}

[CategoryAttribute("ID Settings"), DescriptionAttribute("Address of the customer")]
public string Address
{
get
{
return _address;
}

set
{
_address = value;
}
}

[CategoryAttribute("ID Settings"), DescriptionAttribute("Date of Birth of the Customer (optional)")]
public DateTime DateOfBirth
{
get
{
return _dateOfBirth;
}

set
{
_dateOfBirth = value;
}
}

[CategoryAttribute("ID Settings"), DescriptionAttribute("Age of the customer")]
public int Age
{
get
{
return _age;
}

set
{
_age = value;
}
}

[CategoryAttribute("Marketting Settings"), DescriptionAttribute("If the customer has bought more than 10 times, this is set to true")]
public bool FrequentBuyer
{
get
{
return _frequentBuyer;
}

set
{
_frequentBuyer = value;
}
}

[CategoryAttribute("Marketting Settings"), DescriptionAttribute("Most current e-mail of the customer")]
public string Email
{
get
{
return _email;
}

set
{
_email = value;
}
}
[CategoryAttribute("Color"), DescriptionAttribute("Most current ForeColor of the Label")]
public string ForeColor
{
get
{
return _forecolor;
}

set
{
_forecolor = value;
}
}

[CategoryAttribute("Color"), DescriptionAttribute("Most current BackColor of the Label")]
public string BackColor
{
get
{
return _backcolor;
}

set
{
_backcolor = value;
}
}

[CategoryAttribute("Font"), DescriptionAttribute("Most current FontSize of the Label")]
public double FontSize
{
get
{
return _fontsize;
}

set
{
_fontsize = value;
}
}


public Customer()
{
//
// TODO: Add constructor logic here
//
}
}
}


6)Run the Application

No comments: