Posted by : ජේ Thursday, September 26, 2013

Today I am going to discuss about very useful tips in WPF application developing.
1) How to validate a Text Box using regular expressions.

This is very easy no more need to use if else blocks and lengthy codes to validate the Text Boxes only need regular expressions.


· Add this to top of the class
using System.Text.RegularExpressions;
· Method that implement the validation for the textbox
If you use this method to a Text box it can be typed only numbers
private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("[^0-9.-]+");
            e.Handled = regex.IsMatch(e.Text);
        }

· Then bind the method to a relevant Text box
<TextBox Name="txtVara" PreviewTextInput="NumberValidationTextBox"/>

2) How to navigate to a video or web site using button

Sometimes we want to navigate websites and videos outside the WPF application let’s see how to do it.

string path = new System.IO.FileInfo("WebHelp/QGSHelp.htm").FullName;
System.Diagnostics.Process.Start(path);
What I do is simply pass the file path. :)

3) How to use web control tool in WPF

Many interfaces are implemented with websites. In a WPF program, we provide access to websites (and other data sources) with the WebBrowser control. WebBrowser uses the Internet Explorer rendering engine.

<WebBrowser x:Name="Browser" HorizontalAlignment="Left" Height="228" Margin="10,222,0,0" VerticalAlignment="Top" Width="450"/>

How to navigate a web to a web Browser control
Browser.Navigate("http://en.wikipedia.org/");

How to navigate a web page stored inside hard disk
Browser.Source = new Uri(new System.IO.FileInfo("TexWeb/Tex.html").FullName);

4) WPF toolkit

Extended WPF Toolkit™ is the number one collection of WPF controls, components and utilities for creating next generation Windows applications. Use it to build professional looking, modern, and easy to use line of business applications. This is very useful tool kit set for developing WPF applications

Download from:- https://wpftoolkit.codeplex.com/

5) How to add objects to the Data Grid

First make a class for objects that you want to add to the Data Grid

public class test
       {
       
        public string question { getset; }
        public string answer { getset; }
        public string wanswer1 { getset; }
        public string wanswer2 { getset; }
        public string wanswer3 { getset; }
}

Add the Data Grid

DataGrid x:Name="dataGrid" >
       <DataGrid.Columns>
       <DataGridTextColumn Binding="{Binding question}"  Header="Question"/>
        <DataGridTextColumn Binding="{Binding answer}"  Header="Answer"/>
       <DataGridTextColumn Binding="{Binding wanswer1}"  Header="Wrong Answer1"/>
       <DataGridTextColumn Binding="{Binding wanswer2}"  Header="Wrong Answer2"/>
        <DataGridTextColumn Binding="{Binding wanswer3}"  Header="Wrong Answer3"/>
                </DataGrid.Columns>
            </DataGrid>

Add object to the data grid
dataGrid.Items.Add(new test { question ="ques", answer ="ans", wanswer1 = "w1", wanswer2 = "w2", wanswer3 = "w3"});

6) Add template Column to the Data Grid

                <DataGrid >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="header"/>
                 <DataGridTemplateColumn Header="Image" >
                  <DataGridTemplateColumn.CellTemplate>
                   <DataTemplate>
                  <Image Height="114" Width="163" Source="{Binding imgPath}" ></Image>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>

7) How to convert image to base 64 string and How to convert Base64 string to Image

Image to base 64
private string getBase64(string path)
        {
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, (int)fs.Length);
            fs.Close();
            return System.Convert.ToBase64String(data);
        }
Base64 to Image
private BitmapImage GetImage(string base64String)
        {
            byte[] binaryData = Convert.FromBase64String(base64String);
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.StreamSource = new MemoryStream(binaryData);
            bi.EndInit();
            return bi;
                    }

8) View Box Control

The ViewBox is a very useful control in WPF. If does nothing more than scale to fit the content to the available size. It does not resize the content, but it transforms it. This means that also all text sizes and line widths were scaled. It’s about the same behavior as if you set the Stretch property on an Image or Path to Uniform. This is the solution for resolution problems.

Although it can be used to fit any type of control, it's often used for 2D graphics, or to fit a scalable part of a user interface into a screen area.
<Button Content="Test" />
  <Viewbox Stretch="Uniform">
     <Button Content="Test" />
</Viewbox>




9) MediaElement

If you want play a video inside the wpf application better option is use a MediaElement.
It’s quite easy to add a video player into a WPF .xaml window.
Drag and drop the 'MediaElement' to the .xaml window.

On the form loaded event, write the following code
mediaElement1.Source = new Uri(@"F:\Media\Memory Card\Media\Video\2.MP4");

Change the 'uri' path to the location where you stored your favorite videos.

10) How to use specific Windows theme in a WPF application

WPF includes all common Windows themes. By default WPF loads the current Windows theme as your default style-set. But you can override these styles by loading a specific theme. To do this you first have to add a reference to the style assembly you like to use and second you need to merge the theme resource dictionary into your app resources. This overrides the default style-set that has been loaded by WPF.

The following example shows how to load the Windows Vista Aero theme. Open App.xaml and add this.
<App.Resources>
    <ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0,
     Culture=neutral, PublicKeyToken=31bf3856ad364e35,
rocessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />
</App.Resources>



Leave a Reply

Subscribe to Posts | Subscribe to Comments

Popular Post

Total Pageviews

Powered by Blogger.

- Copyright © sharpish j -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -