How to give style to wpf controls

3 comments
Here you can see we are giving style to wpf controls in design page only . You will get complete idea about static style in wpf applications. Let's see,

Main window
<Window x:Class="StyleDemo.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">
    <Window.Resources>
        <Style x:Key="textblock_style" TargetType="TextBlock">
            <Setter Property="HorizontalAlignment" Value="Right"></Setter>
            <Setter Property="Margin" Value="5"></Setter>
         
        </Style>
        <Style x:Key="textbox_style" TargetType="TextBox">
            <Setter Property="HorizontalAlignment" Value="Left"></Setter>
            <Setter Property="Margin" Value="5"></Setter>
            <Setter Property="Width" Value="150"></Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="2*"/>
        </Grid.ColumnDefinitions>
        <TextBlock Text="Name" Grid.Row="0" Grid.Column="0" Style="{StaticResource textblock_style}"></TextBlock>
        <TextBox  Grid.Row="0" Grid.Column="1" Style="{StaticResource textbox_style}"></TextBox>
        <TextBlock Text="Address" Grid.Row="1" Grid.Column="0" Style="{StaticResource textblock_style}"></TextBlock>
        <TextBox  Grid.Row="1" Grid.Column="1" Style="{StaticResource textbox_style}"></TextBox>
        <TextBlock Text="description" Grid.Row="2" Grid.Column="0" Style="{StaticResource textblock_style}"></TextBlock>
        <TextBox  Grid.Row="2" Grid.Column="1" Style="{StaticResource textbox_style}"></TextBox>
    </Grid>
</Window>
Let's see output


Read More...

Implement key frames in wpf appication

Leave a Comment
create simple key frames animation in wpf application. I will give you demo with code also here so you can learn easily from here.

Main Window.
<Window x:Class="DesignSideAnnimation.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>
        <Button Content="Animated Me!!!" Width="50" Height="40" >
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard  >
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Width" Duration="0:0:5">
                                    <LinearDoubleKeyFrame Value="50" KeyTime="0:0:0"/>
                                 
                                    <LinearDoubleKeyFrame Value="200" KeyTime="0:0:3"/>
                                    <LinearDoubleKeyFrame Value="450" KeyTime="0:0:4"/>
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                           
                        </BeginStoryboard>
                       
                    </EventTrigger.Actions>
                   
                   
                </EventTrigger>
               
               
            </Button.Triggers>
           
           
        </Button>
    </Grid>
</Window>
Output:


Read More...

Simple design side animation example in wpf application

Leave a Comment
Simple design side animation example in wpf application is here. We don't need to explain it but you need to put this code snippets in design page only.

Mainwindow
<Window x:Class="DesignSideAnnimation.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>
        <Button Content="Animated Me!!!" Width="50" Height="40" >
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard TargetProperty="Width" >
                                <DoubleAnimation From="50" To="250" Duration="0:0:5" AutoReverse="True"></DoubleAnimation>
                            </Storyboard>
                         
                        </BeginStoryboard>
                     
                    </EventTrigger.Actions>
                 
                 
                </EventTrigger>
             
             
            </Button.Triggers>
         
         
        </Button>
    </Grid>
</Window>
Output will look like this.


Read More...

implement brush demo in wpf application

Leave a Comment
implement brush demo in wpf application with complete source code with example. Here we will see very small example with code in wpf application.
Main window
<Window x:Class="Brushdemo.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>
        <Rectangle Height="200" Width="200">
            <Rectangle.Fill>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black" Offset="0"/>
                    <GradientStop Color="#FFD60F0F" Offset="0.972"/>
                    <GradientStop Color="#FFCFD7DA" Offset="0.427"/>
                    <GradientStop Color="#FFCFD7DA" Offset="0.279"/>
                    <GradientStop Color="#FED2B577" Offset="0.663"/>
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
    </Grid>
</Window>
Now let's see the brush demo output


Read More...

Simple Ball moving animation in wpf

Leave a Comment
Here we will show you create complete animation idea in wpf application. Here you can see that we provide you code snippets for it.

MainWindow
<Window x:Class="Animation.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">
    <Canvas>
        <Ellipse Height="50" Width="50" Fill="red" Name="animationobject" Canvas.Top="20" Canvas.Left="20" />
    </Canvas>
</Window>
MainWindow.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Animation
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DoubleAnimation annimation = new DoubleAnimation();
            annimation.From = 0;
            annimation.To = 450;
            annimation.Duration = new Duration(TimeSpan.Parse("0:0:3"));
            annimation.AutoReverse = true;
            annimation.RepeatBehavior = new RepeatBehavior(TimeSpan.Parse("0:0:18"));
            animationobject.BeginAnimation(Canvas.LeftProperty,annimation);
        }
    }
}
See the output of this demo but it is not looking animated in snap but we are providing you source code also just download and run it.



Read More...

Create Shap Animations in wpf application

Leave a Comment
Create Different types of shapes with animations in wpf application. Here i will give you complete source code with demo. Let's start to see the code,

Main window
<Window x:Class="ShapeAnimation.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">
    <Canvas>
        <Rectangle Height="100" Width="50" Fill="Red" Canvas.Top="100" Canvas.Left="100" Stroke="Yellow" StrokeThickness="5"></Rectangle>
        <Rectangle Height="50" Width="50" Fill="Red" Canvas.Top="100" Canvas.Left="200" Stroke="Yellow" StrokeThickness="5"></Rectangle>
        <Ellipse Height="50" Width="50" Fill="Red" Canvas.Top="100" Canvas.Left="270" Stroke="Yellow" StrokeThickness="5"></Ellipse>
        <Line X1="0" Y1="0" X2="300" Y2="300" Stroke="Green" StrokeThickness="5"></Line>
        <Polyline Points="120,120,125,140,250,190,200,205,120,120" Stroke="Black" StrokeThickness="5"/>
        <Polyline Points="220,220,225,240,350,290,300,305,220,220" Fill="pink" Stroke="Black" StrokeThickness="5"/>
    </Canvas>
</Window>
Now you can see all types of shapes which we have created using design part of wpf application.



Read More...

how to show word document in viewer using wpf application?

Leave a Comment
Hi friends here i will show you complete code with example to show you word document in viewer in wpf application. Let's start to learn document viewer,

Main window
<Window x:Class="DocumentShow.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>
        <Grid.RowDefinitions>
            <RowDefinition Height="70"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Label Name="lable1" Margin="3,6,0,0" Content="Show Word File:" VerticalAlignment="Top" HorizontalAlignment="Left" />
        <TextBox  Name="txt_filename" VerticalAlignment="Top"  HorizontalAlignment="Stretch" Margin="110,10,300,0" HorizontalContentAlignment="Left" />
        <Button HorizontalAlignment="Right" VerticalAlignment="Top" Width="150" Content="Select File(word)" Name="wordbutton" Margin="0,10,130,0" Click="wordbutton_Click"  />
        <Button HorizontalAlignment="Left" Margin="407,10,0,0" VerticalAlignment="Top" Content="View Word File" Width="100" Name="viewdoc" Click="viewdoc_Click" />
        <DocumentViewer Name="dc" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,37,0,0" Grid.RowSpan="2"/>
    </Grid>
</Window>
MainWindow.cs
using System;
using System.IO;
using System.Windows;
using System.Windows.Xps.Packaging;
using Microsoft.Office.Interop.Word;
using Microsoft.Win32;
using Word = Microsoft.Office.Interop.Word;
namespace DocumentShow
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void wordbutton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            // Set filter and RestoreDirectory
            openFileDialog.RestoreDirectory = true;
            openFileDialog.Filter = "Word documents(*.doc;*.docx)|*.doc;*.docx";
            bool? result = openFileDialog.ShowDialog();
            if (result == true)
            {
                if (openFileDialog.FileName.Length > 0)
                {
                    txt_filename.Text = openFileDialog.FileName;
                }
            }
        }
        private XpsDocument ConvertWordToXps(string wordFilename, string xpsFilename)
        {
            // Create a WordApplication and host word document
            Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
            try
            {
                wordApp.Documents.Open(wordFilename);
                // To Invisible the word document
                wordApp.Application.Visible = false;
                // Minimize the opened word document
                wordApp.WindowState = WdWindowState.wdWindowStateMinimize;
                Document doc = wordApp.ActiveDocument;
                doc.SaveAs(xpsFilename, WdSaveFormat.wdFormatXPS);
                XpsDocument xpsDocument = new XpsDocument(xpsFilename, FileAccess.Read);
                return xpsDocument;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error occurs, The error message is  " + ex.ToString());
                return null;
            }
            finally
            {
                wordApp.Documents.Close();
                ((_Application)wordApp).Quit(WdSaveOptions.wdDoNotSaveChanges);
            }
        }
        private void viewdoc_Click(object sender, RoutedEventArgs e)
        {
            string wordDocument = txt_filename.Text;
            if (string.IsNullOrEmpty(wordDocument) || !File.Exists(wordDocument))
            {
                MessageBox.Show("The file is invalid. Please select an existing file again.");
            }
            else
            {
                string convertedXpsDoc = string.Concat(System.IO.Path.GetTempPath(), "\\", Guid.NewGuid().ToString(), ".xps");
                XpsDocument xpsDocument = ConvertWordToXps(wordDocument, convertedXpsDoc);
                if (xpsDocument == null)
                {
                    return;
                }
                dc.Document = xpsDocument.GetFixedDocumentSequence();
            }
        }
    }
}
Let's see the output snap of it,



Read More...

How to run gif image in wpf application?

Leave a Comment
Hi friend this is the very new example of gif image which will be run in wpf application. I will show you complete example source code for running gif image in this demo.

Main window.
<Window x:Class="gifImagedemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:gif="http://wpfanimatedgif.codeplex.com"
        gif:ImageBehavior.AnimateInDesignMode="True"
        Title="MainWindow" Height="570.37" Width="525">
   
        <StackPanel>
        <Image Name="processgif" gif:ImageBehavior.RepeatBehavior="Forever" gif:ImageBehavior.AnimatedSource="Get-Ready.gif"/>
        </StackPanel>
   
</Window>
This code snippets is sufficient to run gif image in wpf.
Output:


Read More...

How to create Menu bar in wpf application?

Leave a Comment
Create Menu Bar in wpf with this simple demo example of menu bar with us. I am keeping source code and code snippets for menu bar also. Let's see what's it will give us as output,

Mainwindow

<Window x:Class="MenuBar.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>
        <Menu>
            <MenuItem Header="File1">
                <MenuItem Header="file11"/>
                <MenuItem Header="File12"/>
                <MenuItem Header="File13">
                    <MenuItem Header="file11" IsCheckable="True"/>
                    <MenuItem Header="File12"/>
                </MenuItem>
            </MenuItem>
            <MenuItem Header="File2">
                <MenuItem Header="file11"/>
                <MenuItem Header="File12"/>
                <Separator/>
                <MenuItem Header="File13" >
                    <MenuItem Header="file11" IsCheckable="True"/>
                    <MenuItem Header="File12" />
                </MenuItem>
            </MenuItem>
        </Menu>
    </Grid>
</Window>
Now this is the time for output,


Read More...