Tag: Silverlight 4.0

  • Silverlight Deferred / Delayed Selected Date Picker

    Silverlight Deferred / Delayed Selected Date Picker

    When implementing a date picker that is used to fetch some ‘details’ based on the selected date, the chances are that you don’t want to begin fetching the ‘details’ immediately.

    If you did, and made a rapid change to the SelectedDate (maybe holding down an arrow key), the asynchronous fetch will be repeated a large number of times.

    To prevent this, implement a deferral on the selected date change, for example:

    public class DeferredDP : DatePicker {
    
    public DeferredDP() {
    this.DefaultStyleKey = typeof(DatePicker);
    base.SelectedDateChanged += new EventHandler(DeferredDP_SelectedDateChanged);
    }
    
    DispatcherTimer _timer;
    
    void DeferredDP_SelectedDateChanged(object sender, SelectionChangedEventArgs e) {
    /// if there is an instance of the timer already running stop it.
    if (_timer != null) {
    _timer.Stop();
    _timer = null;
    }
    
    /// only trigger the selection changed event when the date has been changed
    if (e.AddedItems.Count == 1 &&
    e.AddedItems[0] is DateTime) {
    
    if (SelectionDelay.HasValue) {
    /// if the timer delay has been set, delay the setting of the value
    _timer = new DispatcherTimer();
    _timer.Interval = new TimeSpan(0,0,0,0,SelectionDelay.Value);
    _timer.Tick += (s1, e1) => {
    SetValue(SelectedDateDeferredProperty, (DateTime)e.AddedItems[0]);
    if (_timer != null) {
    _timer.Stop();
    _timer = null;
    }
    };
    _timer.Start();
    } else { // if the timer delay is not set, set the property immediately.
    SetValue(SelectedDateDeferredProperty, (DateTime)e.AddedItems[0]);
    }
    }
    }
    
    ///
    /// Milliseconds delay before the selected date value is updated.
    ///
    public int? SelectionDelay {
    get { return (int?)GetValue(SelectionDelayProperty); }
    set { SetValue(SelectionDelayProperty, value); }
    }
    
    public static readonly DependencyProperty SelectionDelayProperty =
    DependencyProperty.Register("SelectionDelay", typeof(int?), typeof(DeferredDP), new PropertyMetadata(null));
    
    public DateTime? SelectedDateDeferred {
    get { return (DateTime?)GetValue(SelectedDateDeferredProperty); }
    set { SetValue(SelectedDateDeferredProperty, value); }
    }
    
    public static readonly DependencyProperty SelectedDateDeferredProperty =
    DependencyProperty.Register("SelectedDateDeferred", typeof(DateTime?), typeof(DeferredDP), new PropertyMetadata(null, SelectedDateDeferredChanged));
    public static void SelectedDateDeferredChanged(DependencyObject d, DependencyPropertyChangedEventArgs args) {
    DeferredDP instance = d as DeferredDP;
    
    if (instance.SelectedDateDeferred != instance.SelectedDateDeferred) {
    instance.SelectedDateDeferred = instance.SelectedDateDeferred;
    }
    
    }
    }
    
  • Silverlight Tab Stops – IsTabStop

    Just found a tricky little quirk with TabStop that caused a fair bit of confusion. It turns out that the default implementation of ContentControl overrides Control, so implements the IsTabStop property, and defaults that value to true.

    ContentControl is implemented by the delightful BusyIndicator, so if you have used a BusyIndicator within the scope of a series of input controls you’ll see the Invisible Tab Stop behaviour.

    HINT: Using Silverlight Spy shows us where the offending property lies. You’ll need to re-template that to set the IsTabStop property to false.

  • New Silverlight Juicyness

    Only 3 months after the release of SL3 comes the beta for SL4.  And to be honest it just gets better and better. Following on from David Poll’s excellent PDC session (Building Line Of Business Applications with Silverlight 4) the following bits really got my juices flowing!;

    “Learn about enhancements to data binding and data validation as well as new support for rich text & printing in the platform that allow you to build compelling LOB user experiences. In addition, you will see how you can incorporate webcam & microphone support into your applications using Silverlight 4.”

    This was a good “hand-on” session from David Poll, Silvelight Program Manager. As always, there was a lot of cut-and-paste code, but most concepts where demonstrated well enough to get a grasp of what was required, and the MVVM pattern was talked about on many occasions.

    Interactive Design Surface in VS2010

    The design surface in VS2010 is interactive for XAML controls. This will allow the proofing and creation of layouts from within VS (rather than being forced to switch to Blend). Most properties (including element Binding) can be set from within VS visual design surface. This didn’t crash during the demo – hopefully this will be more stable than the early versions of Blend.

    Data-binding Enhancements

    Binding now supports string formatters – very useful feature for eg enabling the formatting of strings inline in the XAML using standard .NET format codes (e.g. {0:d}) rather than having to create separate format classes.

    
    

    Binding to dictionaries is also added, again simplifying the development process to display controls for simple enumeration types.

    Binding support has been extended to include any DependencyObject, not just objects which derive from UIElement.

    New values have been introduced to binding to support a “FallbackValue” and “TargetNullValue” which will handle the displayed values in situations where a property setter exception occurs and the value is set to Null respectively.

     

    Data Related Controls

    Datagrid now supports “star” based columns. This allows columns to fit the available screen space – much more akin to what we are used to with HTML tables.

    Datagrid supports copy and paste – good potential for copying information to Excel. I have also seen (in the Day 2 keynote) that there is support for calls directly into the Office API. Export to excel will either be possible in SL4 or available in the very near future.

    Combobox will support both SelectedValue and SelectedValuePath – which have been missing to this point. This will simplify the XAML used to display simple comboboxes, which was overly onerous for such a simple control.

    The CollectionViewSource type is enhanced to provide data grouping (this would be ideal for large datagrids of information (case enquiry / allocation).

    Commanding has been introduced as a first-class property on appropriate controls (ButtonBase and Hyperlink). We will not need to attach the commanding properties from the Prism assemblies.

    Input Validation

    Validation is now supported from a service, and responses sent back to the UI via INotifyDataErrorInfo. This is a massive improvement to the validation API and will be hugely beneficial for our product. We no longer have to rely on exceptions in property setters to trigger UI validation controls. Errors raised in the service can be bubbled into the UI easily and displayed using the existing validation controls. Dependency validation can also be achieved in this way.

    Printing Support

    Printing support is added to native applications. This will allows relatively simple setup of printing direct from the UI.

    Any UI element can be printed, and it does not need to exist in the currently displayed Visual Tree.

    Paging is supported and appeared straight forward to control.

    Other Features

    Other interesting functionality in the SL4 beta but not included within this presentation, includes:

    • Implicit Styling – no more explicit style references J
    • Right Click and Mouse Wheel support
    • Native Drag and Drop
    • Clipboard API
    • New controls

      • Viewbox control added (for auto scaling content)
      • Rich text box

    • Hosting of HTML content
    • More advanced out of browser features

      • Notification (“Toast”) API (ideal for an Activity Manager style application!)

    • Trusted mode is enabled
    • Webcam access (e.g. barcode scanning from hand held devices)
    • Official Google Chrome Support