Friday, March 30, 2012

Priority Binding, WPF

Consider a scenario in which you've a base class A and there are two classes B & C derived from the class A. Also base class A has a virtual property named 'DisplayName' which represents the name of object of that class (Insignificant what it does). Now you want to show this DisplayName on UI, say in a textbox.
Simply setting Text = "{Binding DisplayName}" can serve this purpose. Quite Simple

Now say class B has a particular type and for that instead of showing DisplayName you need to use DisplayNameSpecific property. How you will achieve this?

Problem: How to specify two binding sources for a simple binding and use the one which holds priority.
We can do this by using Converters, in converter we can check if the current object type is of Class B then show DisplayNameSpecific else use DisplayName. It will work smoothly but there is one more smarter way to achieve this, wiz Priority Binding

Priority Binding:
MSDN defines it as: Priority Binding class describes a collection of binding objects that is attached to a single binding target property, which receives its value from the first binding in the collection that produces a value successfully. PriorityBinding lets you associate a property with a list of bindings. The first binding that returns a value successfully becomes the active binding.

The value DependencyProperty.UnsetValue is not considered a successful return value.

NOTE: The binding engine continues to listen for changes on all bindings. If at any point one of the bindings that has a higher priority returns a value successfully, the value for that binding becomes the active value and replaces the current value.

Thus in our example we'll do something like:
                    <Binding Path="DisplayNameSpecific" />
                    <Binding Path="DisplayName"  />

That's it. :)

Wednesday, March 28, 2012

Creating a hyperlink with both text and link, WPF

[WPF, C#] We generally use hyperlink in order to navigate to certain URL but have you ever wonder how you'll display a string with hyperlink in it? E.g. In help menu of your application, you need to show something link "For more information please contact 123-456-7890 or email". In this string, 'email' is a hyperlink, clicking on which open the outlook.

There is no simple or built in control for such thing and even you can't do this by clubbing text & hyperlink together in a grid. Here is a workaround to achieve this in code behind of your XAML file.

Add a hyperlink in xaml and don't specify it's text/content there, instead the content is specified using code behind in constructor of that usercontrol.

// string you need to show on GUI
string contactSupport = "For more information please contact 123-456-7890 or email";
// place holder constant. Content which you want to create hyperlink
const string EMAIL_PLACEHOLDER = "email";
string[] stringSeparators = new string[] {EMAIL_PLACEHOLDER };
// create hyperlink which will be called on clicking 'email'
Hyperlink hyperlink = new Hyperlink(){ 
NavigateUri = new URI("mailto:")
// text to show for hyperlink content. Email here
hyperLink.RequestNavigate += Hyperlink_RequestNavigate;

// hyperlink created now you need to break the text string into two string one will be displayed as simple text and another will be hyperlink.
string beforeText = contactSupport.Split(stringSeparators, StringSplitOptions.None)[0];
string afterText = contactSupport.Split(stringSeparators, StringSplitOptions.None)[1];
//Now you've both text and hyperlink add them in the hyperlink which you've created in xaml
// 1. First part of text is added to that hyperlink. It will be displayed as simple text
this.supportHyperlink.Inlines.Add(new Run(beforeText));
// 2. Add hyperlink
// 3. Add any other text, if any you want to add after the hyperlink.
this.supportHyperlink.Inlines.Add(new Run(afterText));

Now, you're ready to use the hyperlink with both simple text & hyperlink.

Friday, March 23, 2012

Viewing contents of a DataSet or DataView in quick watch, C#

[Visual Studio] Have you ever faced any issue in visualizing the contents of a dataset while debugging a problem in Visual Studio? Generally we've a habit of  viewing the contents of a collection while debugging a particular problem but you can't do the same with DataSet. Here is a quick tip to view DataSet contents in QuickWatch.

Visual Studio Visualizers
This is what MSDN says about visualizers: A visualizer creates a dialog box or another interface to display a variable or object in a manner that is appropriate to its data type. For example, an HTML visualizer interprets an HTML string and displays the result as it would appear in a browser window; a bitmap visualizer interprets a bitmap structure and displays the graphic it represents. Some visualizers enable you to modify as well as view the data.
The Visual Studio debugger includes five standard visualizers.
1,2,3. The text, HTML, and XML visualizers, all of which work on string objects;
4. the WPF Tree visualizer, for displaying the properties of a WPF object visual tree;
5. the dataset visualizer, which works for DataSet, DataView, and DataTable objects. This is the one we should use in order to solver our problem.

Visualizers are represented in the debugger by a magnifying glass icon. When you see the magnifying glass icon in a DataTip, in a debugger variables window, or in theQuickWatch dialog box, you can click the magnifying glass to select a visualizer appropriate to the data type of the corresponding object.

Following figure will explain the way to use it:

Also note that: Visualizers are not supported on the Compact Framework.

Friday, March 16, 2012

Assigning a dynamic resource link programmatically

In XAML, you can specify whether a resource is static resource or dynamic by using the StaticResource and DynamicResource with binding. Also if you want to do this thing pro grammatically (in C#) then static behavior is simple. You just need to find the resource using TryFindResource() method and set the desired property of it. But if you want to have it dynamically updated then use SetResourceReference() method.

frameworkElement.SetResourceReference( dependencyProperty, resourceKey );


TFS: Compare with workspace version

Microsoft's Team Foundation Server provides different ways to compare your edited file with different versions. Among them are:
1. Unmodified Version
2. Workspace Version
3. Latest Version

Option 1 & 3 are self-explanatory however it is worth noticing the difference between WorkSpace version & Latest version.
WorkSpace version VS Latest Version
Comparing with workspace version actually compares the edited file with the version of file at the time you checked out. But comparing with latest version compares the edited file with latest version of file present at your code repository i.e. it might include changes made by your colleagues.

E.g. Say you check out a file on 1st november and did some changes till 3rd november. Now on 3rd november if you try to compare your edited file with Workspace version tfs will compare the edited file with the file present in tfs on 1st november (i.e. the time at which you checked out the file). It might be possible that your fellow colleagues made some changes to that file till 3rd november. So comparing the edited file with latest file with show those changes, basically it will compare the edited file with the latest version of that file.

In most of the scenario, programmers work on latest code (and it's a good practice that you atleast take latest code once in a day) that's why both option results in same output.

Thursday, March 15, 2012

Why and how you should create flexible applications?

What is a loosely couple application? Ever wonder why design architects always try to enforce application design to be loosely coupled even when you are confident that you can write a bug free app?
We should not develop an app which is completely loosely coupled. How you should evaluate the measure of coupling your application have and does it needs improvement or not?
What are the benefits of loosely couple application?

To answer here is a very good post by James Kovacs :Tame Your Software Dependencies for More Flexible Apps.

Link for detailed explanation of design pattern to use for building flexible composite application (very nice article, I would say must read for programmers working on windows application development) : Prism : Patterns For Building Composite Applications With WPF

Happy Reading.

Instantiating a view model in view (XAML)

You can create an instance of viewmodel in view (XAML) in order to create data binding at design time for a data context that is assigned at run time using DESIGN INSTANCE.

Syntax: d:DataContext="{d:DesignInstance Type=local:ABC}"

More information about it on MSDN article : Click here