WPF Form without using XAML

Ddd
Posted by Ddd under WPF category on | Points: 40 | Views : 2594
1)Open a WPF Application called as WpfApplication1.
2)Add a class from Add New Item and put the following code.

3)Please delete the default Window1.xaml file.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
namespace WpfApplication1
{

public partial class Window1 : Window
{
private Button button1;
public Window1()
{
InitializeComponent();
}
private void InitializeComponent()
{
// Configure the form.
this.Width = this.Height = 285;
this.Left = this.Top = 100;
this.Title = "Code-Only Window";
// Create a container to hold a button.
DockPanel panel = new DockPanel();
// Create the button.
button1 = new Button();
button1.Content = "Please click me.";
button1.Margin = new Thickness(30);
// Attach the event handler.
button1.Click += button1_Click;
IAddChild container = panel;
container.AddChild(button1);
// Place the panel in the form.
container = this;
container.AddChild(panel);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
button1.Content = "Thank you.";
}
}

}

//To execute this code, add another class to the Application and add this
//Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace WpfApplication1
{
public partial class Program : Application
{
[STAThread()]
static void Main()
{
Program app = new Program();
app.MainWindow = new Window1();
app.MainWindow.ShowDialog();
}
}

}


Now go to WPFApplication1->Properties->Open->StartUpObject->WpfApplication1.Program.

When you click the Button, its text should change to Thank you

Comments or Responses

Login to post response