in

Dé specialist in .NET trainingen en consultancy

Jo-wen Mei

december 2009 - Posts

  • Invoking an ICommand

     

    I wanted to reuse the logic of a Command defined in another usercontrol.

    The simple solution would be to create a separate, public method which I then could call from outside the control (and call this method in the Execute eventhandler).

    A more clean way is to directly invoke the Command!

    The ICommand interface supports the Execute(object parameter) method.

    I expose my commands as static properties, so I can have this code:

    SearchCommands.FilterProduct.Execute(null, SearchControl);

     

    I don’t have to reference the commandBinding collection of the search control, which is very clean!

    You can/must specify the input-element with the implementation you want to invoke.....

     

    more info

    Posted dec 30 2009, 07:40 by Jo-wen with no comments
    Filed under:
  • WPF & xaml guidelines

     

    Wpf and xaml has been out for a while now, so the community already knows the do’s and don’ts of programming in xaml.

    Thankfully, there are always some nice people who share this stuff with us.

    This is a good resource and starting point. It also includes links to the channel 9 videos that give you some background info on the rationale of these guidelines.

     

    Some other guidelines/info:

    Designing with WPF

    XAML Guidelines for Creating a Composite UI

    Posted dec 30 2009, 07:10 by Jo-wen with no comments
    Filed under: ,
  • Grid Size Sharing WPF

     

    The other day, I had to show different datatemplates depending on a specific property value. (The datatemplateSelector is your friend!)

    I used different foreground colors to visualize the states; however, the layout design was the same for each datatemplate.

     

    I was using a Grid, and wanted to share the columnWidth’s and RowHeight’s.

    My first solution was specifying a double constant in xaml. Unfortunately, this doesn’t work out of the box. You would need to have a DoubleToGridLengthConverter.

    Then, after some searching I found this Grid Size Sharing sample!

    Basically, you have to define the attached Grid.IsSharedSizeScope on the container element to set the scope of the shared sizes.

    Then when you set the SharedSizeGroup on a row/column definition (together with the width/height of course), other definitions with the same name will automagically adjust as well.

     

    nice!

    Posted dec 16 2009, 10:32 by Jo-wen with no comments
    Filed under: ,
  • Check some (runtime) characteristics of a dependency property

     

    It's pretty easy to inherit from an existing control in WPF.

    For instance, this great programmer I know has written an extended combobox. It has extra functionality, like:

    * Automatically select the value if only one item is available in the items source;

    * Disable the control when no items are in the items source;

    So, there's logic in the inherited class that manages the IsEnabled property.

    Flexibility must remain, so consumers of this control should still be able to set the IsEnabled property, and overrule the default logic.

    Here's a way to programmatically check if the property has been set:

    if (DependencyPropertyHelper.GetValueSource(this, ComboBox.IsEnabledProperty).BaseValueSource == BaseValueSource.Default)

    {

          IsEnabled = !(IsDisabledWhenItemsSourceIsEmpty && (_customItemsSource.Count == 0));

    }

    More info

    Posted dec 11 2009, 05:52 by Jo-wen with no comments
    Filed under:
  • When not implementing the ConvertBack method…

    I really like WPF converters. I often don’t need the ConvertBack method though.

    Most programmers deal with this by leaving the throw new NotImplementedException() , or they return null.

     

    I also see this method being marked with the [Obsolete()]  attribute. I think this totally gives you the wrong impression that it should not be used anymore.

    The attribute is used to inform programmers , not the databinding system.

    And of course, the method is still valid and might get a different implementation later on.

     

    Check out the DependencyProperty.UnsetValue.

    Posted dec 10 2009, 09:33 by Jo-wen with no comments
    Filed under: ,
  • WPF vs Silverlight

    Last week, I was giving a Silverlight training in the beautiful Bergen (Norway).

    I have WPF experience, but I was still struggling with some demo’s because of its differences with Silverlight.

    Silverlight is supposed to be a subset of WPF but that’s not the case (yet). Silverlight even has functionalities that are not in WPF (yet), like the Visual State Manager.

    There’s a nice whitepaper that describes these differences.

    Posted dec 06 2009, 09:38 by Jo-wen with no comments
    Filed under: ,