Anup Shah on WPF and Silverlight (Programming Garden)

IT 's For You!!!

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

Saturday, September 14, 2013

Galaxy Blog

10:54 PM

Friday, September 6, 2013

http://www.galaxywebmind.com/ SUCCESSIVE LAUNCH:

9:11 AM
SUCCESSIVE LAUNCH:
http://www.galaxywebmind.com/

GALAXY WEB MIND & THE SMS PANEL ON LIVE 
http://sms.galaxywebmind.com/

Wednesday, August 28, 2013

http://www.galaxywebmind.com/

11:24 PM
What we are awaiting is now come true...

The Galaxy Group of Companies
added new venture on SMS platform galaxywebmind.com





www.galaxywebmind.com

Pure Web/Windows/Mobile/SMS/Marketting/ Solutions

Thank you.

Wednesday, February 27, 2013

WPF: Showing Tooltip on a disabled element

2:34 PM


WPF: Showing Tooltip on hovering over disabled element


To show the tooltip while hovering over disabled element, use the ShowOnDisabled attached property of the ToolTipService class! From XAML.
Syntax:



<Button ToolTipService.ShowOnDisabled="True"></Button>



Or from C# code, you can call the static method corresponding to the attached property:
ToolTipService.SetShowOnDisabled(myButton, true);

Note: If you want to close the tooltip that is currently showing then,
Set its IsOpen property to false.

Happy Coding!!!

Wednesday, April 4, 2012

Understanding Predicate Delegates in C#

3:02 PM
Introduction

NET Framework 2.0 came with the concept of Predicate Delegates, but compared to the other features of .NET, it never got the attention it deserved. It is really a powerful concept which makes it easy to write searching algorithms on collections. These are also widely used while performing filter operations on WPF object data binding. In this article, we will take a look into the concepts behind predicate delegates and try and understand their importance.

What is a predicate delegate?

A predicate delegate is a delegate with the following signature:

Return type - bool
Argument type - generic
So, a predicate delegate is a delegate which points to a boolean function that returns true or false and takes a generic type as an argument. A predicate delegate thus is a delegate which is capable of taking any custom type as an argument. This makes it quite useful because what we get as a result is a generic delegate. The bool function that it points to has logic to evaluate any condition and return true/false accordingly.


Why use a predicate delegate?

The most common use of a predicate delegate is for searching items in a collection. Because a predicate delegate is a delegate of type T or generic, it is most useful when searching items in a generic collection. It really comes handy when you have a collection with tons of items and you need to perform a search operation. Using a predicate delegate not only reduces the lines of code that needs to be written, but also increases performance of the application. These are also used while performing filter operations on IcoolectionView objects while performing data binding in WPF.

For example, create a console application and declare an Employee class. Declare these properties for the Employee class: FirstName, LastName, and Designation. See code below:

class Employee
{
private string _firstName;
private string _lastName;
//private int _empCode;
private string _designation;

public Employee()
{ }
public Employee(string firstName, string lastName, string designation)
{
_firstName = firstName;
_lastName = lastName;
_designation = designation;
}
///
/// Property First Name
///
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
///
/// Property Last Name
///
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
public string Designation
{
get { return _designation; }
set { _designation = value; }
}
}

In the Main() method of the console application, declare three employees as below:
// Declare 3 employees
Employee emp1 = new Employee("Anshu", "Dutta", "SSE");
Employee emp2 = new Employee("John", "Doe", "Manager");
Employee emp3 = new Employee("Jane", "Doe", "Assistant");

Create a generic List and put the above employees in the list:

List empList = new List { emp1, emp2, emp3 };

We will demonstrate the use of a predicate delegate by performing a search operation on the Employee List.

Method #1 - The traditional way of doing it

Create a bool function that searches an employee first name and returns true if found:


private static bool EmpSearch(Employee emp)
{
if (emp.FirstName == "Anshu")
return true;
else
return false;
}
Declare a predicate delegate pointing to that function:

Predicate pred = new Predicate(EmpSearch);
Call the Find() method of the List<> and pass the predicate delegate as an argument:
Employee emp = empList.Find(pred);


The Find() method internally iterates through each item in the list and calls the function EmpSearch, passing each item from the list as an argument one by one. When the FirstName of the employee matches the condition, the function returns true. The Find method then returns that employee object to the Employee placeholder emp. That means for each item in the list, the Find() method calls the function EmpSearch passing the items as arguments one by one. Whichever item satisfies the condition in the function is then returned.

Print the result to check:

Console.WriteLine("Employee Found {0}",emp.FirstName);
Console.ReadLine();

Saturday, February 18, 2012

When should I use WPF...?

2:26 PM

 When should I use WPF instead of DirectX? (Is DirectX dead?)


DirectX is definitely not dead and is still more appropriate than WPF for advanced developers writing hard-core “twitch games” or applications with complex 3D models where you need maximum performance. That said, it’s easy to write a naive DirectX application that performs far worse than a similar WPF application.
DirectX is a low-level interface to the graphics hardware that exposes all of the quirks of
whatever GPU a particular computer has. DirectX can be thought of as assembly language in
the world of graphics: You can do anything the GPU supports.

WPF provides a high-level abstraction that takes a description of your scene and
figures out the best way to render it, given the hardware resources available. Internally, this
might involve using Shader Model 3.0, or the fixed-function pipeline, or software. (Don’t worry if you’re not familiar with these terms, but take it as a sign that you should be using WPF!).

The downside of choosing DirectX over WPF is a potentially astronomical increase in development cost. A large part of this cost is the requirement to test your application on each
driver/GPU combination you intend to support. One of the major benefits of building on top of WPF is that Microsoft has already done this testing for you! You can instead focus your
testing on low-end hardware for measuring performance. The fact that WPF applications can
even leverage the client GPU over Remote Desktop or in a partial-trust environment is also a
compelling differentiator.

When should I use WPF instead of Windows Forms?(Is Windows Forms dead?)

WPF is clearly more suitable for applications with rich media, but some people have said that Windows Forms is the best choice for business applications with traditional user interfaces. I think this belief is based on first versions of WPF in which many standard controls didn’t exist (such as TreeView, ListView, and OpenFileDialog) and a visual designer didn’t exist, making traditional Windows application development in WPF harder than in Windows Forms. Although Windows Forms still has useful controls that WPF lacks (such as DataGridView and PropertyGrid) and at the time of writing has a larger set of third-party controls in the marketplace, WPF has compelling features even for traditional user interfaces (such as the support for resolution independence or advanced layout).

When should I use WPF instead of Adobe Flash?

For creating rich web content, Flash is currently the most popular option because of its ubiquity.
You can put Flash-based content on a website with confidence that the overwhelming
majority of visitors already have the necessary player installed. (And if they don’t, it’s a very
quick download.)

WPF applications can also run within a web browser. WPF has the advantage of better development
tools and programming model, a richer feature set, robust control reuse, broad
programming language support, and full access to the underlying platform (when security
permits). But viewing such content requires Windows and the .NET Framework 3.0 (installed
by default on Windows Vista or later).

Friday, February 17, 2012

Anup Shah on WPF and Silverlight: Architecture...

2:22 PM
Anup Shah on WPF and Silverlight: Architecture Of WPF
Above figure shows the overall architecture of WPF. It has three major sections presentation core, presentation framework and milcore. In the same diagram we have shown how other section like direct a...
View or comment on Anup Shah's post »
Google+ makes sharing on the web more like sharing in real life. Learn more.
Join Google+
You have received this message because Anup Shah shared it with anupshah2it.netspider@blogger.com. Unsubscribe from these emails.

Architecture Of WPF

2:18 PM
********************************************************************************
Can you explain the overall architecture of WPF?
********************************************************************************



Above figure shows the overall architecture of WPF. It has three major sections presentation core, presentation framework and milcore. In the same diagram we have shown how other section like direct and operating system interact with the system. So let’s go section by section to understand how every section works.

User32:- It decides which goes where on the screen.

DirectX: - As said previously WPF uses directX internally. DirectX talks with drivers and renders the content.

Milcore: - Mil stands for media integration library. This section is a unmanaged code because it acts like a bridge between WPF managed and DirectX / User32 unmanaged API.

Presentation core ;- This is a low level API exposed by WPF providing features for 2D , 3D , geometry etc.

Presentation framework:- This section has high level features like application controls , layouts . Content etc which helps you to build up your application



Wednesday, February 15, 2012

Ques.

11:44 AM

************************************************************
Difference between DataSet and DataReader 
************************************************************
DataReader 
=========== 
DataReader is like a forward only recordset. It fetches one row at a time so very less network cost compare to DataSet(Fethces all the rows at a time). DataReader is readonly so we can't do any transaction on them. DataReader will be the best choice where we need to show the data to the user which requires no transaction. As DataReader is forward only so we can't fetch data randomly. .NET Data Providers optimizes the datareader to handle huge amount of data.
 

DataSet
 
=======
 
DataSet is an in memory representation of a collection of Database objects including tables of a relational database schemas.
 
DataSet is always a bulky object that requires a lot of memory space compare to DataReader. We can say that the DataSet is a small database because it stores the schema and data in the application memory area. DataSet fetches all data from the datasource at a time to its memory area. So we can traverse through the object to get the required data like querying database.







************************************************************
What is the difference between app.config, web.config and machine.config ?
************************************************************

In this .NET Interview questions interviewer expects two things. First the importance of configuration and second in which scenarios are the above file applicable. So lets answer the question in two parts.

### The importance of config files  ### 

App.config, web.config and machine.config are files which store configuration data in XML

format. We need configuration data at application level or at machine/server level.

### Scenarios in which the above config files are used  ### 

Machine.config file stores configuration information at system level. It can contain configuration information like timeout in ASP.NET application, requestLimit, memoryLimit, and ClientConnectedCheck etc.

Generally we have two kinds of application web application and windows application. Web.config file stores configuration data for web applications and app.config file store configuration information for windows application.

Application level configuration data can be like connection strings,security etc.


************************************************************
What is Encapsulation?
***********************************************************

  • Encapsulation is one of the fundamental principles of object-oriented programming.
  • Encapsulation is a process of hiding all the internal details of an object from the outside world
  • Encapsulation is the ability to hide its data and methods from outside the world and only expose data and methods that are required
  • Encapsulation is a protective barrier that prevents the code and data being randomly accessed by other code or by outside the class
  • Encapsulation gives us maintainability, flexibility and extensibility to our code.
  • Encapsulation makes implementation inaccessible to other parts of the program and protect from whatever actions might be taken outside the function or class.
  • Encapsulation provides a way to protect data from accidental corruption
  • Encapsulation hides information within an object
  • Encapsulation is the technique or process of making the fields in a class private and providing access to the fields using public methods
  • Encapsulation gives you the ability to validate the values before the object user change or obtain the value
  • Encapsulation allows us to create a "black box" and protects an objects internal state from corruption by its clients.

Wednesday, February 1, 2012

DataContext

1:44 PM

WPF provides a simple and powerful way to auto-update data between the business model and the user interface. This mechanism is called DataBinding. Everytime when the data of your business model changes, it automatically reflects the updates to the user interface and vice versa. This is the preferred method in WPF to bring data to the user interface.

Databinding can be unidirectional (source -> target or target <- source), or bidirectional (source <-> target).

The source of a databinding can be a normal .NET property or a DependencyProperty. The target property of the bindingmust be a DependencyProperty.

To make the databinding properly work, both sides of a binding must provide a change notification that tells the binding when to update the target value. On normal .NET properties this is done by raising the PropertyChanged event of theINotifyPropertyChanged interface. On DependencyProperties it is done by the PropertyChanged callback of the property metadata

Databinding is typically done in XAML by using the {Binding} markup extension. The following example shows a simple binding between the text of a TextBox and a Label that reflects the typed value:

Every WPF control derived from FrameworkElement has a DataContext property. This property is meant to be set to the data object it visualizes. If you don't explicity define a source of a binding, it takes the data context by default.

The DataContext property inherits its value to child elements. So you can set the DataContext on a superior layout container and its value is inherited to all child elements. This is very useful if you want to build a form that is bound to multiple properties of the same data object.


ValueConverters

If you want to bind two properties of different types together, you need to use a ValueConverter. A ValueConverter converts the value from a source type to a target type and back. WPF already includes some value converters but in most cases you will need to write your own by implementing the IValueConverter interface.

A typical example is to bind a boolean member to the Visibility property. Since the visibility is an enum value that can be Visible, Collapsed or Hidden, you need a value converter.



The following example shows a simple converter that converts a boolen to a visibility property. Note that such a converter is already part of the .NET framework.


 


-> you can derive your value converter from MarkupExtension and return its own instance in the ProvideValueoverride. So you can use it directly without referencing it from the resources.

-> When you get the error "No constructor for type '...' has 0 parameters.", you need to add an default constructor to your converter, even it's not needed. Just for the WPF designer.


WPF - Basic layout using the Grid

1:38 PM

WPF - Basic layout using the Grid

The final result

Your application layout is one of the first things you have to plan before starting the actual development. In this basic tutorial, I will show you how to design a basic layout for your applications using the Grid control.

 

First of all, we will need to create a new WPF Application project. To do so, fire up Visual Studio and select File > New > Project... Choose WPF Application and click OK. Once your project has been created, we will change the size of our application window so we can better see our progress as shown in the following code snippet:

<Window x:Class="WPFBasicLayout.Window1"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Title="WPF Basic Layout" Height="350" Width="500">     <Grid>
     </Grid> </Window>

Notice that Visual Studio automatically places a Grid control for us. The Grid is the most complex layout control and probably the one we will use the most. Our next task is to declare columns and rows definitions inside our Grid. The idea is to have four parts in our application: the header, the footer, a sidebar and the main content.

<Grid.ColumnDefinitions>     <ColumnDefinition />     <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.RowDefinitions>     <RowDefinition />     <RowDefinition />     <RowDefinition /> </Grid.RowDefinitions>

So far, our window should look like this in the Visual Studio designer:

Rows and columns definitions

To keep this tutorial simple, we will use Border and Label controls as the content of the four parts of our application. We will add one Border and a Label for each part of our application. Notice how we place them in the row and column we want using Grid.Row and Grid.Column properties respectively:

<Border Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" BorderBrush="LightBlue" BorderThickness="2,2,2,2" CornerRadius="10,10,10,10" Margin="05,05,05,05">     <Label FontSize="24" VerticalAlignment="Center" HorizontalAlignment="Center">Header</Label> </Border> <Border Grid.Column="0" Grid.Row="1" BorderBrush="LightBlue" BorderThickness="2,2,2,2" CornerRadius="10,10,10,10" Margin="05,05,05,05">     <Label FontSize="24" VerticalAlignment="Center" HorizontalAlignment="Center">Sidebar</Label> </Border> <Border Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" BorderBrush="LightBlue" BorderThickness="2,2,2,2" CornerRadius="10,10,10,10" Margin="05,05,05,05">     <Label FontSize="24" VerticalAlignment="Center" HorizontalAlignment="Center">Footer</Label> </Border> <Border Grid.Column="1" Grid.Row="1" BorderBrush="LightBlue" BorderThickness="2,2,2,2" CornerRadius="10,10,10,10" Margin="05,05,05,05">     <Label FontSize="24" VerticalAlignment="Center" HorizontalAlignment="Center">Main Content</Label> </Border>

Notice also the Grid.ColumnSpan property. That means that that specific column will take two columns, beginning from the one it was placed. This happens with the header and the footer because we want then to take all the width they have available.

In future posts, I will show you how to move those repeated properties into Styles for cleaner code.

Our application is getting the look we are looking for, but as you can see we got some problems: the header and the footer are too big and the sidebar and the main content are taking the same space. What we want is a smaller header and footer and a thinner sidebar, so the room left is taken by the main content. In order to get the desired effect, we need to add Width and Height properties to some of the ColumnDefinition and RowDefinition elements respectively. We will set those properties to Auto for the the first column and for the first and third row. That way, the header, the footer and the sidebar will resize accordingly based on the size of their content. All the room left will be available for the main content.

<Grid.ColumnDefinitions>     <ColumnDefinition Width="Auto"/>     <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.RowDefinitions>     <RowDefinition Height="Auto"/>     <RowDefinition />     <RowDefinition Height="Auto"/> </Grid.RowDefinitions>

Now it is time to run our application and check the final result. As you can see, the layout is pretty basic but quite common and useful. Notice how the different parts of our application are resized accordingly when the application is resized or maximized.

Now that you have this basic layout set up, it is up to you to add controls, images and text to improve the results.



Tuesday, January 31, 2012

WPF Commanding: The Basics

2:53 PM

Commanding in WPF is unlike your traditional events in WinForms. Commands were primarily designed to be used at the application level, but they have become one of the most popular features among developers when it comes to UI programming (we tend to use them more than they should be). Commands enable the developer to define a task once and “attach” it multiple times without having to go through the traditional means which would require duplicating the code or even calling it in more than one place. (This is the magic.) WPF intrinsically provides five commands that you can use out of the box.

  1. ApplicationCommands
  2. ComponentCommands
  3. EditingCommands
  4. MediaCommands
  5. vNavigationCommands

I have explored them and have taken a liking to ApplicationCommands. (In fact this is the one that most developers will use.)

To use the command, do the following:

  1. Link your custom/predefined command to a control that you want to respond to the command and add an input gesture to the command.
  2. Create a handler for the command and use the CommandBindings class to bind the handler to the control.
  3. Add the binding to the control’s Commands collection.

So let’s delve into some code to demonstrate WPF commanding.


WPF Basics

2:52 PM

Definition

“The Windows Presentation Foundation is Microsofts next generation UI framework to create applications with a rich user interface.”

History

Before the release of WPF, UI developers used to concentrate on various services or API to develop desktop applications to produce simple application to high end graphic application. So it was highly necessary for any developer to concentrate on one or more services or API for building high end graphics.

Developers used to concentrate on many API’s to achieve desired functionality and make UI more attracitve

Forms Dialog boxes ,controls — VB6 MFC

2D Graphics System.Drawing.dll

3D Graphics DirectX

Streaming Video Windows Media Player API

FLow/Mixes Documents PDFs API

In WPF all these is achieved with only Windows Presentation Foundation Framework