WPF Hyperlink Button Style

Here is an implementation to style a button as a link button in WPF


                

Setting Initial Focus in WPF Form

In WPF dialog boxes, I quite often see some code which looks like the following code. example.

private void OnLoaded(object sender, EventArgs e)
{
    this._textSearch.Focus();
}

This is usually accompanied by some XAML that likes like the following XAML example.

<Window ...
        Loaded="OnLoaded">

Occasionally, I also see the event being setup up in the constructor. For some reason this really gets on my nerves. I much prefer to see event handlers defined in the XAML. I’m not quite sure what my objection is; I think I just prefer to see all the UI things in one place.

What this actually achieves (as you can probably work out), is that the control (in this case a TextBox) named _textSearch has focus when the dialog box is loaded. In actual fact, WPF provides a much easier way of setting the initial focus by using the FocusManager. The FocusManager provides a set of static methods, properties, and events that you use to determine and set the focus scope[1] and to set the focused element within the focus scope.

So, using the FocusManager, you don’t need to handle the Loaded event and manually calling the Focus method as shown in the previous code examples, you can simply set the FocusedElement on the FocusManager as shown in the following code example.

<Window ...
        FocusManager.FocusedElement="{Binding ElementName=_textSearch}">

 

Simple, neat, all in one place, and no code-behind 🙂

5 day Silverlight Training at SSD India

SSD

SSD is a Microsoft certified partner company located in West Bengal, Kolkata India. They required an extensive training for there team in Silverlight. From 25th of July till 29th of July, i conducted the Silverlight training covering a vast range of topics like Basics of Silverlight, WPF, MVVM, RIA Services, Business Application Development, Expression Blend, dependency properties, Routed events, Layouts, Animations, Transformations and bonus content like window phone 7 application development overview.

It was a 5 day training event focusing on an extensive Silverlight course material like Basics of Silverlight, XAML, Animations, MVVM, RIA Services, Business application design, WPF. Although 5 days were not enough to grasp and swallow the whole stuff but i believe it was a good learning experience for the whole SSD Team as well as myself.

Attendees of Silverlight training at SSD

Attendees of Silverlight training at SSD

Complete training details are as below:

Day 1

///////

WPF – An Introduction

.net applications over the years

What to expect?

Some background

What WPF offers?

XAML

Need for XAML

XAML Basics

Properties in XAML

Type Convertors

Visual Tree & render Process

Exercises

Day 2

///////

Layout Controls

Layout Philosophy

Layout process

Layout containers

Exercises

Practical Session

Build a Silverlight WPF application decided at runtime

Dependency Properties

Dynamic Resolution

Attached Properties

Dependency Props

WPF Events

Event fire direction

The content controls

General introduction to controls

Day 3

///////

DataBinding

Validations

DataViews

Formatting bound data

ListView

WPF Commands

Localization

Animation

Property based model

Day 4

///////

RIA Services

Building Business applications using RIA Services

MVVM

Build an application over MVVM architecture

Command Sources

Command binding

Silverlight

Silverlight application Cycle

Build an application using Silverlight and RIA Services

Day 5

///////

Recap ofSession

Introductionto Expression Blend UI

Small demoof how to develop a windows phone 7 app

GoodbyeSession

WPF Routed Events Exposed

What are Routed events? A question that always routed in my mind. The concept is such simple and beautiful. Lets take it with an example from mother nature. Suppose a mother bird comes to the nest with food for her children. One way is she put the food in the nest and the birdlings eat with there own beaks. Other way is the mother feeds each and every child from her own beak. Both methods work, but the later one is more natural, beautiful, efficient and secure.

Similar to routed events in WPF, its very simple. Say there are three buttons in a Grid. Each having its own click event. What that does is it adds some extra code to the code behind for method declarations. What if there is only one function that handles all the click events of buttons on the page. How it works is as below:

//A Common void method to handle all clicks

private void Window_Click(object sender, RoutedEventArgs e)
{
FrameworkElement elem = e.Source as Button;

    MessageBox.Show((elem as Button).Content.ToString());
}

The Xaml would be like this

<Window x:Class=”TestingRoutedEvents.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>
<StackPanel>
<StackPanel ButtonBase.Click=”Window_Click”>
<TextBox x:Name=”Hello” Text=”Hello World”/>
<TextBox/>
<Button Content=”btnok”/>
<Button Content=”btnCancel”/>
<Button Content=”btnExit”/>
</StackPanel>
<StackPanel>
<TextBox/>
<TextBox/>
<Button Content=”btnok1″/>
<Button Content=”btnCancel1″/>
<Button Content=”btnExit1″/>
</StackPanel>
</StackPanel>
</Grid>
</Window>

In Xaml code above, ButtonBase.Click=”Window_Click” does the trick, the routed events use two type of routines to fire, one is bubble-up apprach and other is Tunnel-down approach. A window click event is a bubble up event. So lets say, user clicks on btnok, this event will bubble up the visual tree and see if there is a implementation of routed event ButtonBase.Click=”Window_Click”, if it finds one, if fires that. The above code will not fire for btnok1, as in its visual tree, its direct parent is Stack Panel and StackPanel’s parent is a Window. So if we want that all the buttons should get the routed event, then either we should add ButtonBase.Click=”Window_Click” to both Stack Panels, or only on windows implementation like this;

 <Window x:Class=”TestingRoutedEvents.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″ ButtonBase.Click=”Window_Click”>
<Grid>
<StackPanel>
<StackPanel>
<TextBox x:Name=”Hello” Text=”Hello World”/>
<TextBox/>
<Button Content=”btnok”/>
<Button Content=”btnCancel”/>
<Button Content=”btnExit”/>
</StackPanel>
<StackPanel>
<TextBox/>
<TextBox/>
<Button Content=”btnok1″/>
<Button Content=”btnCancel1″/>
<Button Content=”btnExit1″/>
</StackPanel>
</StackPanel>
</Grid>
</Window>

The myth of routed events i hope is now resolved.

Library project file cannot specify ApplicationDefinition element.

Strange line for an error. By reading it, you will get lost, and finding it is another mad thing to do. Anyways, i sorted this out. I was working on a silverlight project, i accidently or intentionally copied the image files to web project. I forgot to change a property, i compiled and the error was ringing church bells in my ear.

So the solution is, the image i copied has the property Build action set to Application Resource. I set it to content and it worked!! I hope if you get this error in future you will not get stuck.

RIA Services deployment issue

Rich internet applications are handy tool to work with when you’re developing database applications in Silverlight using Entity Framework. RIA services provide a bridge through domain services. Working in it is a dreamy thing, as it does much of plumbing and networking stuff. The pain starts when you deploy it to live site.

I had a site i was to host on Godaddy servers, i published the website and uploaded it over the ftp. Things should have gone smoother, but all of a sudden i got this error:

IIS specified authentication schemes ‘Basic, Anonymous’, but the binding only  supports specification of exactly one authentication scheme. Valid  authentication schemes are Digest, Negotiate, NTLM, Basic, or Anonymous. Change  the IIS settings so that only a single authentication scheme is used.”

I wandered here and there, serached around 20 million webpages (i meant few 100 but it was painful), and i got a solution at last.

The hostings normally by default have both basic and anonymous authentication enabled. Disabling anonymous does not do the trick as it will keep on asking for passwords to authenticate which the poor users do not have. So the solution was to disable basic authentication. Viola it worked like a charm.