Register Login

 

Welcome to the Coach Factor blog. Here you will find all of our ideas on software development. Subscribe at  http://blog.nventive.net.

# Wednesday, November 26, 2008

After presenting at Microsoft TechDays the “BlackBelt WPF DataBinding Session”, we received a very interesting question from one of the attendees:

If there are validation rules associated to a binding declared in XAML and validation fails due to invalid data, the value in the target (“onscreen value”) will not be in sync with the value in the source (your business object).

In the end, the folllowing screen shot shows what we want :

image

  1. The ErrorTemplate is active, because a validation error occured;
  2. The tooltip shows the range of valid values and the invalid value that was entered;
  3. The textbox shows the currently binded value, and not the actual invalid value that the user entered.

By default, the text box would show the invalid value, and would be out of sync with the binded object.

This is what we came up with to change that default behavior: when the validator fails, invoke the binding mechanism as to update the target (onscreen value) with the current value in the source (business object).

Here is the recipe:

  1. Implement INotifyPropertyChanged on your business object.
  2. Create the IRaiseNotifyPropertyChanged:

        1     public interface IRaiseNotifyPropertyChanged

        2     {

        3         void Raise(string PropertyName);

        4     }

  3. Implement IRaiseNotifyPropertyChanged on your business object as follows:

        1         public void Raise(string PropertyName)

        2         {

        3             if (PropertyChanged != null)

        4             {

        5                 PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));

        6             }

        7         }

  4. Add these two properties to the class that implements your validation rule (derives from ValidationRule):

        1         public string PropertyName { get; set; }

        2         public object Target { get; set; }

  5. Modify the Validate method like this:

        1             if ((age < Min) || (age > Max))

        2             {

        3                 if (Target is IRaiseNotifyPropertyChanged)

        4                 {

        5                     IRaiseNotifyPropertyChanged t = Target as IRaiseNotifyPropertyChanged;

        6                     t.Raise(PropertyName);

        7                 }

        8 

        9                 return new ValidationResult(false,

       10                   "Please enter an age in the range: " + Min + " - " + Max + ". You entered : " + value.ToString());

       11             }

       12             else

       13             {

       14                 return ValidationResult.ValidResult;

       15             }

  6. Use the validation rule like this in your XAML:
    <c:agerangerule target="{Binding}" propertyname="Age" max="130" min="21">

We hope that this will be useful in your WPF adventures !

Wednesday, November 26, 2008 10:37:00 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
.net - WPF
# Tuesday, November 04, 2008

DevTeach is coming soon, in 3 weeks in fact, and we wanted to mention it because of WPF. As you know, nVentive is strongly pushing the WPF technology so that we can start businesses can start building LOB applications with it.

I hadn't realized it before, but the first day (extra fee) has a full session with Kevin McNeish, where he will spend the day with Expression Blend. As if that wasn't enough, there are over 6 other sessions during the conference that will speak of WPF (or Silverlight).

Once done, don't forget to come and talk to us about your experience. nVentive is growing and will be on site to discover hidden talents.

Tuesday, November 04, 2008 8:52:42 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
.net - WPF
# Friday, October 24, 2008

As announced previously, nVentive will be presenting at the Montreal Tech Days, brought to you by Microsoft on November 6th and 7th. Make sure the register, as this event is not a free one.

Erik will be giving the "Blackbelt Data Binding in WPF" presentation (make sure your to bring your karate skills) and François will be giving the "Building Differentiated UI Applications Using Composite WPF" talk. As a sneak preview, you can also catch François for the Tech Days in Toronto, as he will be doing his presentation there also.

These presentations should help your business intergrate the latest and greatest technologies from Microsoft for presenting information to your users. While François` talk will concentrate with some of the "best practices" given by Microsoft`s Patterns and Practices group on how to build composite applications, Erik`s will dig deeper into the misteries of data binding.

We hope to see you there, and as always, will be available to answer any questions that you might have on these new technologies.

Friday, October 24, 2008 7:53:21 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
.net - WPF | Announcement
# Tuesday, October 21, 2008

When we were in Quebec city last week training a group of people on WPF, one of the trainees asked a question :

   How would I go and change the "presentation" of the scroll bar.

This questioned was asked during one of the labs and so we had time (10 minutes) to come up with a quick answer, here is the walkthrough that we presented:

  1. Subclass the ScrollBar control by deriving a new class from it, don't forget to add this code in the static constructor so that styles gets hooked up correctly :

       18             DefaultStyleKeyProperty.OverrideMetadata(typeof(MyScrollBar),

       19                 new FrameworkPropertyMetadata(typeof(MyScrollBar)));

  2. Load up Reflector and locate the the PresentationFramwork.Aero.dll assembly in the GAC. This assembly contains WPF resource dictionaries that WPF merges into the application scope when a WPF application starts. WPF loads the correct dll "theme" file according to the OS that is running.
  3. Install the BamlViewer addin for Reflector because the DLL doesn't contain readable XAML. It is stored by the compiler in a binary format in order to optimize loading and storage of these massive XML files.
  4. Locate the section in the converted XAML that pertains to the ScrollBar control, copy that into your own resource section, rename a few things to "MyScrollBar" and voilà, a custom scrollbar.

This is the 1000 feet view on how to do skinning, and will require a lot more work when creating a custom style that works correctly (handling commands, events, triggers...).

It was a mere introduction to demonstrate the simplicity of the model that WPF uses to "present" controls.

Tuesday, October 21, 2008 8:20:53 PM (Eastern Standard Time, UTC-05:00)  #    Comments [1] -
.net - WPF
# Sunday, October 19, 2008

Last week, Erik gave a 2 day training in Quebec city on the topic of WPF. He was amazed to see just how much interest there currently is for this new technology, especially now that SilverLight 2 got released.

These are some of the questions that were asked, as well as pointers to the answers:

1 - What do you recommend, a XBAP application or a SiliverLight one ?

   This is a hard one because it involves so many upfront decisions. If you do not control the computers that will run the application, a SiliverLight application is the easiest to deploy. At the same time, SilverLight applications have severe constraints (partial trust, doesn't benefit from the full .net framework, stripped down WPF, limited network access...) but benefits from advances like DeepZoom (checkout the DeepEarth project). XBAP applications are normal .net applications that are hosted in the browser (Internet Explorer) and are usually partial trust (as the assemblies are downloaded at run-time from a webserver).

2 - Should we write new controls ?

   WPF comes with so many extension points (skins, themes, control templates, data templates...) that should all be evaluated before actually writing a new control. In a upcoming post, we will explain the "on the fly walkthough" we used to change how scroll bars were displayed for a "trendy application". Writing new controls is still a possibility, allthough we think you will find that there are better ways to extend WPF.

3 - Isn't SilverLight just for making cheesy animations on the web ?

   SilverLight 1.0 was only able to work with WPF (through XAML) and was mostly used to display media. SilverLight 2.0 comes with an implementation of the .net framework which allows you to develop a lot of the same kinds of applications you do with the .net framework; think of it in the same way as you would the Compact Framework. One major difference with the Compact Framework (the one that runs on a Windows Mobile phone) is that there is no binary compatibility for assemblies and so code has to be physically recompiled. Microsoft believes so much in the SilverLight plateform for business applications that they are porting prism and unity to it (see here and here).

Sunday, October 19, 2008 7:59:11 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
.net - WPF
Search
Archive
<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567
Statistics
Total Posts: 47
This Year: 0
This Month: 0
This Week: 0
Comments: 2
Sign In