Friday, January 3, 2014

How to add dependency property for custom control in wpf

Introduction

Ever had problem that your custom controls dependency property is not getting updated when you changed it from the control? Probably most of the wpf developer already know it still sharing it because, I kind of lost few minutes in darkness when I found that my custom control is not updating the target dependency property.

Enough talk show me the code

Well here is the code!. Do you find anything wrong with it? Seems absolutely fine.

public static readonly DependencyProperty SelectedValueProperty =
DependencyProperty.Register(
"SelectedValue", typeof(DateTime?), typeof(DateTimePicker), new UIPropertyMetadata(null, OnPropertyChanged));

public DateTime? SelectedValue
{
get
{
return (DateTime?)this.GetValue(SelectedValueProperty);
}
set
{
this.SetValue(SelectedValueProperty, value);
}
}

Now, my view model has a notify property which looks like the below code


public DateTime CurrentDate
{
get
{
return this.currentDate;
}
set
{
this.currentDate = value;
this.OnPropertyChanged("CurrentDate");
}
}

And the data is hooked via xaml binding, code below.


<riteq:DateTimePicker IsEnabled="True"
VerticalAlignment
="Top"
SelectedValue
="{Binding CurrentDate}"
x:Name
="TimePicker"
DisplayMode
="DateOnly"
CalendarSelectionMode
="SingleDate">
</riteq:DateTimePicker>

Look a perfect setup for the code. And at first glance we didn’t had any problem, until we tested it. No matter what date I select from my custom date picker, it never updating the CurrentData property of the view model.


So there is something wrong with the controls property. I tested and debug the code seems to be the binding expression working fine.


Then I found a piece of code where I set the dependency property’s value. As soon as i set some thing there the property’s binding got broken.


Here is the code.


if (this.cal.IsDateChanged)
{
this.SelectedValue = this.cal.SelectedDate;
}

Its a simple property setting , but this was causing problem, then when crazy with googling and found an every easy fix for my problem.


And the problem is with my definition of the dependency property. I was using “UIPropertyMetadata”. I changed the definition of property change call back to  “FrameworkPropertyMetadata”, and every thing back to normal and working again. here is the revised defination for the dependency property.


public static readonly DependencyProperty SelectedValueProperty =
DependencyProperty.Register(
"SelectedValue",
typeof(DateTime?),
typeof(DateTimePicker),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPropertyChanged));

So now we are using FrameworkPropertyMetadata as property meta data. And that is the fix for my problem.


Lesson

If you need any dependency that can be changed inside the control please use FrameworkPropertyMetadata for dependency property definition.

Friday, December 6, 2013

Boolean dependency property and its default value.

What’s up community, today I going to share a very basic trick that I learned while working on a wizard control. I would rather say that is a convention that i didn’t know previously. probably all the the WPF developer knows it, but just in case anyone needed it and wondering why its not working.

Enough talk, lets get in to the business, so i was developing a custom control, precisely a reusable wpf wizard control. And I introduced a dependency property for two mode of the UI here is how it looks.

public bool ShouldIgnoreStep
{
get { return (bool)GetValue(ShouldIgnoreStepProperty); }
set { SetValue(ShouldIgnoreStepProperty, value); }
}

public static readonly DependencyProperty ShouldIgnoreStepProperty =
DependencyProperty.Register(
"ShouldIgnoreStep", typeof(bool), typeof(RiteqWizardPage),
new PropertyMetadata(false, OnPropertyChanged));

private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
RiteqWizardPage riteqWizardPage
= d as RiteqWizardPage;
if (e.Property.Name.Equals("ShouldIgnoreStep"))
{
if (e.NewValue != e.OldValue)
{
if (riteqWizardPage != null)
{
riteqWizardPage.OnShouldIgnoreStepChanged();
}
}
}
}


I started to use the property and set the properties value in codes. The first one that i used in the same container worked okay. But the subsequent controls in the same container was not getting property set as I have dependent code when the dependency property is changed I wish to do some work. But the OnPropertyChanged event on the other instance of the control returning always the default value.


I was about the pull all my hairs out, but suddenly it strikes in my head, the problem is the default value that I set to the dependency property.


Then I tried with default value to “null”, and it worked!. Wallah!. Well offcourse I had to use nullable bool to be able to set its default value to null.


In my opinion you should always use default property as to null unless you have some condition that requires to set default value.


public bool? ShouldIgnoreStep
{
get { return (bool?)GetValue(ShouldIgnoreStepProperty); }
set { SetValue(ShouldIgnoreStepProperty, value); }
}

public static readonly DependencyProperty ShouldIgnoreStepProperty =
DependencyProperty.Register(
"ShouldIgnoreStep", typeof(bool?), typeof(RiteqWizardPage),
new PropertyMetadata(null, OnPropertyChanged));

private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
RiteqWizardPage riteqWizardPage
= d as RiteqWizardPage;
if (e.Property.Name.Equals("ShouldIgnoreStep"))
{
if (e.NewValue != e.OldValue)
{
if (riteqWizardPage != null)
{
riteqWizardPage.OnShouldIgnoreStepChanged();
}
}
}
}

Cheers! and happy coding.

Tuesday, November 26, 2013

Optimize the size of xap file an easy way.

Your silverlight projects .xap file is getting bigger, this happens because of some assembly issues. And ofcourse there are some ways to optimize it.

http://blogs.telerik.com/miromiroslavov/posts/10-09-15/5-ways-to-reduce-your-xap-size.aspx

The above article shows few popular ways to reduce the size of xap file.

I had a xap file what was almost 4 mb in size. so I decided to do some optimization.

First thing i did is to remove all unused references and control references. fortunately resharper has some good feature on this. I right clicked on the project and selected “Optimize Reference” and removed all unnecessary references.

Now my xap file is reduce to 1.6 mb. that is almost 200% reduced. awesome.

image

Next there are some option for assembly caching, if you go you the silverlight tab of the project you would see that there is a checkbox for “Reduce XAP size by using…….” Just check it and recompile it. 

image

Next what happen is all the other assemblies that is been used are zipped and added in client bin folder. and reduce the xap file size to negligible.

image

But that does not help that much I guess as the total is still about 1.6 mb. any its a feature and we can used it.

Until next time cheers!.

Update: Checkout this kick ass article to get crazy on size reduction

http://blogs.telerik.com/xamlteam/posts/08-05-13/increasing-the-compression-ratio-of-the-xap-files.aspx

Saturday, October 19, 2013

Unlock password protected pdf files: pdfunlock.

I have been doing some works with pdf files auto fill with itextsharp. one of the file that client gave me was secure and password protected. When ever I try to open this using Adobe Acrobat Pro. Its shows that it have to be opened by Adobe life cycle designer.

Then I installed Adobe life cycle designer and tried opening it, unfortunately end up getting a password window and couldn’t  proceed.

Then of course googled it and found this awesome web where you can unlock pdf online.

http://www.pdfunlock.com/

Cheers!

Thursday, September 12, 2013

Visual studio 2012 Blue Theme

Dear Community, I believe lot of the die hard fan of Microsoft development platform  wondered why visual studio 2012 got rid of the blue theme, in my personal opinion i really liked it, then while working and viewing videos in channel 9  I discovered that we can have different different colors theme in visual studio, and that’s  been there from the beginning.

Man I am stupid, should have figured it out that they have some kind of plan for that. Any way the easies way to get this blue theme going again in visual studio is to use extension.

How to get the blue theme?

Go to visual studio 2012’s extension manager and then search for “Color” in online category.

image

You will find couple of interesting extortions, Download the “Visual Studio 2012 Color Theme Editor”

image

The download and install will begin instantaneously, after that the Visual studio will be required to restart.

After restart you will get option like the below where you can choose your color theme. Choose the right one. In my case I have chosen “Blue”.

image

You can also customized the theme and create your own theme, but that is a whole other story. Until next time. Happy coding.

Friday, August 2, 2013

Team foundation server windows explorer extension

We all used svn that’s for sure for personal use or perhaps company policy to use it doesn't matter, in source control race TFS is getting behind. At lease i felt it, but some recent improvements now taking this back bench product to front bench.

For instance we can have git like behavior in tfs if we want, cool right?. But today I wish to share a different type of tool that can ease the tfs experience a little bit better, in fact its a windows explorer extension, named “Microsoft Visual Studio Team Foundation Server 2012 Power Tools”

You guys can download it from “http://visualstudiogallery.msdn.microsoft.com/b1ef7eb2-e084-4cb8-9bc7-06c3bad9148f

I have used it and its kind of can give you a feeling like tortoise. It has overlay icons just like svn.

image

Now some critic, the extension has relatively slow in performance, at least in my system. It doesn't have branching or switching stuff like other source control extensions, its primitive but at least now we have an extension.

I got excided to realize that now i can add documents folders and other stuff in tfs without need to open visual studio. so its cool and if we all start using it hopefully more feature will be available in future.