Friday, December 16, 2011

Silverlight Shows the default white color on click of any button: Reason and Solution

Generally we have a problem in Wp7. When we click on any button the default White Color comes in view, Event occur and the button Shows again.
this behaviour is due to pressed visual state of silverlight button controll. So we need to modify this Visual state property to come back from this problem.

For this we will add a template in button and will modify its pressed state property. Follow the following Steps to achive the above:
  1. Create an Application and add a simple button in it by using VisualStudio 2010
  2. Select Required Image on it
  3. Save the file 
  4. Now open this Project in Expression Blend(To Open blend Right-Click on Project in solution explorer--> choose Open In Expression Blend). The project will open in Expression blend.
  5. Now select the Button--> Go to Object Menu--> Select Edit Template--> Select Edit Copy...
  6. A dialogue box (Create Style Resource)  opens
    1. Select Apply to all from Name Key
    2. Select Application from Define In (This option create the Application Resource in App.Xaml file)
    3. Click on OK
  7. Select Button Again then goto States menu on left side or on top (depends where it is present)
  8. Click on pressed state panelin CommenStates
  9. Goto Object and Timeline window--> Select the button from template
  10. Goto Properties window
  11. Open Brushes tab --> Click on the square box of Background property--> New Dialogue box opens Named Background--> Goto Template binding--> select Background
  12. It will show the Your default Image in template button
  13. Save the file.
  14. Now open Visual Studio(Do not close the expression Blend)
  15. In Visual Studio It will ask to save the all updated file outside the VS. Dont worry Click on Yes to all button.
  16. Run Your Application.
   By using this method you didnot need to set any property in button. Just use the button as we do previous.

I put my best to make this example if you got any error in above description. Please inform me with your comment. Your suggestions are always welcome.

Thanks

MaxLength property of a textbox with multiline mode doesn't work. Reason and solution

When a Textbox is in SingleLine mode, it gets rendered as a input type text  and when it is in MultiLine mode, it gets rendered as a textarea. 
As we know MaxLength property works only for input type. Thats why MaxLength property doesn't works with Textbox with multiline property
So We have many custom solution for this.
but the best solution is using
Regular expression validator for this, which will check the count of the entered character and will throw an error if it exceeds. We need to have a regular expression that will work. Let's see the code

<asp:TextBox ID="tb" 
runat="server" 
TextMode="MultiLine" >
</asp:TextBox>
//Regular Expression validator
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" 
runat="server" 
ControlToValidate="tb" 
ErrorMessage="Please enter maximum 10 charachters."
                                SetFocusOnError="true" 
ValidationExpression="^[a-zA-Z.]{0,10}$">
</asp:RegularExpressionValidator>


Note: We have used a regular expression ^[a-zA-Z.]{0,10}$, which allows all the characters with length 0 to 10.

Total Pageviews