NumToBoolConverter - A simple IValueConverter implementation

Some 'Beginner's' stuff. One good thing about WPF is the ability to specify custom converters, when you do the binding.

Here is a simple IValueConverter implementation, to convert an int value (1 or 0) to corresponding bool values (true or false).

Useful if you want to bind a check box or something to a property of numeric type.

 
public class NumToBoolConverter : IValueConverter
    {

        #region IValueConverter Members

        public object Convert(object value, Type targetType, 
            object parameter, System.Globalization.CultureInfo culture)
        {
            if (value!=null && value is int )
            {
                var val = (int)value;
                return (val==0) ? false : true;
            }
            return null;

        }

        public object ConvertBack(object value, Type targetType, 
            object parameter, System.Globalization.CultureInfo culture)
        {
            if (value!=null && value is bool )
            {
                var val = (bool)value;
                return val ? 1 : 0;
            }
            return null;
        }

        #endregion
    }

Now, to do the binding in Xaml, you can just create a resource,

<Window.Resources >
        <local:NumToBoolConverter x:Key="IntConverter"/>
    </Window.Resources>

And then do the binding. Simple and easy.

<CheckBox Name="chkBox" 
IsChecked="{Binding Path=IntProperty,Mode=TwoWay, 
Converter={StaticResource IntConverter}}"/>
Read more >>

PDF to XAML Conversion - Alternate Thoughts

Today I did a small experiment to see how we can use PDF to generate XAML files. I wanted to mimic the pdf form entry in Silverlight.

Here is the summary of what I've done.

  • Created a new Silver light Project in Expression Blend 3.0 Beta
  • As Expression Blend can import adobe illustrator files, I simply renamed a pdf file's extension from pdf to ai
  • Clicked File-> Import Adobe Illustrator files in Blend, and imported to my active window.
  • Cool, I've the whole form imported as graphical paths, preserving the whole layout.




I just modified the layout a bit so that it can be embedded it in a canvas in a scoll viewer. So that I can place Silverlight controls (textbox/checkbox etc) on top of that to Mimic form entry. Looks good.
Read more >>

Back To Basics - Thinking Beyond ToString()



About Back To Basics - It's often good to look back and fill any gaps we might be having, in certain aspects of the language or framework we use. I'll be examining and blogging about some well known features of C# and .NET, in Back To Basics series - Probably in a bit more detail.




If you want to convert something to string, you might need to find the converter to do the job correctly.

Here is a neat extension method for all your objects, so that it'll find the appropriate converter if one exists, or otherwise, fall back to ToString() :)

public static class ConverterExtension
    {
        public static string ConvertToString(this object value)
        {
            TypeConverter converter = 
              TypeDescriptor.GetConverter(value.GetType());

            // Can converter convert this type to string?
            if (converter.CanConvertTo(typeof(string)))
            {
                // Convert it
                return converter.ConvertTo(value, 
                        typeof(string)) as string;
            }

            return value.ToString();
        }
    }

Now, try this code, and find out what is the result
Color val = Color.Red;

//Call our extension method
string result1 = val.ConvertToString();

//Try with ToString()
string result2=val.ToString();

See what is in result1 and result2, so that you can make out what I'm talking about. Simple, but useful when you work with scenarios where you want to serialize your object's properties.
Read more >>

TFS API Insights

As I was recently working with TFS Client APIs, here are few thoughts on the same.

Logging Into TFS Server

To log in to the TFS Server using the TFS Client APIs, you can either use the domain authentication, or provide the username or password explicitly

- Logging in using the current windows user

TeamFoundationServer _tfsServer = new TeamFoundationServer("serverUrl");
_tfsServer.Authenticate();

- Logging in using a specific network credential

 _tfsServer = new TeamFoundationServer(
                        "serverUrl", 
                       new System.Net.NetworkCredential
                               ("userName", "password", "domain"));
 _tfsServer.EnsureAuthenticated();

Getting all the projects - Using VersionControlServer

After logging in, you can get all the projects and project items in TFS as shown below. We are reading the latest version for each item

VersionControlServer _versonControlServer=_tfsServer.GetService
                (typeof(VersionControlServer)) as VersionControlServer;

 foreach (var teamProject in 
             _versonControlServer.GetAllTeamProjects(true)) 
 {
       //Get all the items in the project
       ItemSet items = 
              _versonControlServer.GetItems
               (teamProject.ServerItem, VersionSpec.Latest, 
                                         RecursionType.OneLevel);
       foreach (Item item in items.Items)
                        { //do your stuff with each project item }
 

 }

Reading all users - Using GroupSecurityService Now, you can read all users using the IGroupSecurityService, as shown.

   IGroupSecurityService _groupSecurityService = 
_tfsServer.GetService(typeof(IGroupSecurityService)) 
             as IGroupSecurityService;

Identity identity = _groupSecurityService.ReadIdentity
  (SearchFactor.EveryoneApplicationGroup, null, QueryMembership.Expanded);

foreach (Identity user in _groupSecurityService.ReadIdentities
          (SearchFactor.Sid, identity.Members, QueryMembership.None))
                {
                   //do something with the users
                }
TFS API is very simple and intuitive, and in next post I'll explain some work item related methods.
Read more >>

Haha, How to develop software.

This is a nice one. Click Full screen mode and enjoy

Re-visit my article on Architecture Considerations, it might be interesting.

Read more >>

top