Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Thursday, April 11, 2013

Silverlight MVVM

What is MVVM pattern as per wikipedia "The Model View ViewModel (MVVM) is an architectural pattern used in software engineering that originated from Microsoft as a specialization of the Presentation Model design pattern introduced by Martin Fowler.[1] Largely based on the model–view–controller pattern (MVC), MVVM is a specific implementation targeted at UI development platforms which support the event-driven programming in Windows Presentation Foundation (WPF) and Silverlight on the .NET platforms using XAML and .NET languages. Technically different, but similar, Presentation Model design patterns are available in HTML5[2][3] through KnockoutJS, and for Java the ZK framework (Model-View-Binder)."







ViewModelBase.cs



using System;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MVVMDEMO.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            var propertyChanged = PropertyChanged;

            if (propertyChanged != null)
            {
                propertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }



    }
}

Saturday, March 30, 2013

Draw Fractal tree in wpf

In this post I will show you how to draw Fractal tree in WPF.Before going to implementation details first go throw this link.

Algorithm:To draw a fractal tree is simple:
  1. Draw the trunk
  2. At the end of the trunk, split by some angle and draw two branches
  3. Repeat at the end of each branch until a sufficient level of branching is reached



Create a new WPF application and add following code





<Window x:Class="HelloWorld.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Viewbox Stretch="Uniform">
            <StackPanel>
                <StackPanel Orientation="Horizontal"
Margin="5,5,5,0">
                    <Button Name="btnStart" Click="btnStart_Click"
Width="50" Content="Start"/>
                    <TextBlock Name="tbLabel" Margin="20,5,0,0"/>
                </StackPanel>
                <Canvas Name="canvas1" Width="300" Height="300"
Margin="5"/>
            </StackPanel>
        </Viewbox>

    </Grid>
</Window>






using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;

namespace HelloWorld
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int depth = 0;
        private int i = 0;
        private double lengthScale = 0.75;
        private double deltaTheta = Math.PI / 5;
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //Application.Current.Dispatcher.BeginInvoke(
            //                DispatcherPriority.Background,
            //                new Action(() => DrawSine()));


        }
        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            canvas1.Children.Clear();
            tbLabel.Text = "";
            i = 0;
            depth = 1;
            CompositionTarget.Rendering += StartAnimation;
        }
        private void StartAnimation(object sender, EventArgs e)
        {
            i += 1;
            if (i % 60 == 0)
            {
                DrawBinaryTree(canvas1, depth, new Point(canvas1.Width / 2, 0.83 * canvas1.Height), 0.2 * canvas1.Width, -Math.PI / 2);
                string str = "Binary Tree - Depth = " + depth.ToString();
                tbLabel.Text = str;
                depth += 1;
                if (depth > 10)
                {
                    tbLabel.Text = "Binary Tree - Depth =10. Finished";
                    CompositionTarget.Rendering -= StartAnimation;
                }
            }
        }
        private void DrawBinaryTree(Canvas canvas, int depth, Point pt, double length, double theta)
        {
            double x1 = pt.X + length * Math.Cos(theta);
            double y1 = pt.Y + length * Math.Sin(theta);
            Line line = new Line();
            line.Stroke = Brushes.Blue;
            line.X1 = pt.X;
            line.Y1 = pt.Y;
            line.X2 = x1;
            line.Y2 = y1;
            canvas.Children.Add(line);
            if (depth > 1)
            {
                DrawBinaryTree(canvas, depth - 1, new Point(x1, y1), length * lengthScale, theta + deltaTheta);
                DrawBinaryTree(canvas, depth - 1, new Point(x1, y1), length * lengthScale, theta - deltaTheta);
            }
            else
                return;
        }
        //private void DrawSine()
        //{
        //    for (int i = 0; i < 71; i++)
        //    {
        //        double x = i * Math.PI;
        //        double y = 40 + 30 * Math.Sin(x / 10);
        //        polygon1.Points.Add(new Point(x, y));
        //    }
        //}
    }
}




How to draw Sierpinski triangle in WPF

In this post,I will show you how to draw Sierpinski triangle in wpf.
Create a new WPF application and add foloowing code in MainWindow.xaml




<Window x:Class="Sierpinski.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Viewbox Stretch="Uniform">
            <StackPanel>
                <StackPanel Orientation="Horizontal"
Margin="5,5,5,0">
                    <Button Name="btnStart" Click="btnStart_Click"
Width="50" Content="Start"/>
                    <TextBlock Name="tbLabel" Margin="20,5,0,0"/>
                </StackPanel>
                <Canvas Name="canvas1" Width="300" Height="300"
Margin="5"/>
            </StackPanel>
        </Viewbox>

    </Grid>
</Window>


Open MainWindow.xaml.cs and add following code into it


using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

namespace Sierpinski
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int depth = 0;
        private int i = 0;
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {

        }
        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            canvas1.Children.Clear();
            
            CompositionTarget.Rendering += StartAnimation;
        }
        private void StartAnimation(object sender, EventArgs e)
        {
            i += 1;
            if (i % 40== 0)
            {

                Point p1 = new Point(canvas1.Width / 2, 10);
                Point p2 = new Point(10, canvas1.Height - 10);
                Point p3 = new Point(canvas1.Width - 10, canvas1.Height - 10);
                DisplayTriangle(canvas1, p1, p2, p3, depth);
                depth += 1;
                if (depth > 5)
                {

                    CompositionTarget.Rendering -= StartAnimation;
                }
            }

        }


        private void DisplayTriangle(Canvas canvas, Point p1, Point p2, Point p3, int deep)
        {

            if (deep > 0)
            {
                

                // Get the midpoint on each edge in the triangle
                Point p12 = midpoint(p1, p2);
                Point p23 = midpoint(p2, p3);
                Point p31 = midpoint(p3, p1);
                // Recursively display three triangles
                DisplayTriangle(canvas, p1, p12, p31, deep-1);
                DisplayTriangle(canvas, p12, p2, p23, deep-1);
                DisplayTriangle(canvas, p31, p23, p3, deep-1);
                
            }
            else
            {
                // Draw a triangle to connect three points
                DrawLine(p1, p2, canvas1);
                DrawLine(p1, p3, canvas1);
                DrawLine(p2, p3, canvas1);
                return;
                

            }
        }

        private void DrawLine(Point From, Point To, Canvas TargetCanvas)
        {
            Line line = new Line();
            line.Stroke = Brushes.Red;
            line.StrokeThickness = 1.0;
            line.X1 = From.X;
            line.Y1 = From.Y;
            line.X2 = To.X;
            line.Y2 = To.Y;
            TargetCanvas.Children.Add(line);
        }
        private static Point midpoint(Point p1, Point p2)
        {
            return new Point((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2);
        }
    }
}