www.planet-xaml.net
 
  • [WPF]

    Update UI commands from the business logic

    by Florian Krüsch, September 6, 2007

    The command system in WPF is pretty smart. Whenever things change on the UI side it asks the commands bound to UI controls if they still can execute, by calling, well, the CanExecute method.

    This happens, e.g. when a window gets activated or when you click on a button. But what do you do when things don't change on the UI side, but in your middle tier? I needed this in a timed situation, where a thread signals that it collected enough data to enable UI actions.

    There's a brute force way you can go, as described here: DoEvents in WPF. However, this smells a lot like a hack to me and I was looking for something cleaner.

    Turns out, ICommand already defines a CanExecuteChangeEvent that you can use. All it needs is to readd the functionality to RoutedCommand:

    C#RoutedCommandWithRefreshtoggle
    public class RoutedCommandWithRefresh
                   : RoutedCommand, ICommand
    {
       private event EventHandler changed;

       event EventHandler ICommand.CanExecuteChanged
       {
          add 
          { 
             base.CanExecuteChanged += value;
             changed += value;
          }
          remove 
          { 
             base.CanExecuteChanged -= value;
             changed -= value;
          }
       }

       public void Refresh()
       {
          if (changed != null) changed(this, EventArgs.Empty);
       }

       public RoutedCommandWithRefresh(string name, Type owner)
          : base(name, owner)
       {            
       }
    }
    cheers,
    Florian

    1 comment
  •  ^
  • [Silverlight]

    My 2 cents on Silverlight vs. Flash

    by Florian Krüsch, September 7, 2007

    Ted has a post on Silverlight, "thanking" Microsoft for the competition.

    Like previous posts it's just another attempt to stamp Microsoft beeing a propietary company vs. Adobe beeing oh so open.
    He claims VC-1 was a propietary codec, no words about On2 and Sorensen, the propietary codecs Adobe is distributing at this time in its Flash player.

    Do people still believe this Microsoft-evil-empire crap?

    There'll be a Linux version of Silverlight, why? Because the .NET runtime at it's core is an open platform. Because VC-1 is not propietary.

    Silverlight will support various languages, even an open source implementation of Ruby, how open is Flex to developers used to other styles of coding?

    If you want to generate dynamic code for Silverlight, you can just generate XAML as plain text, from any webscript in any language you like - how open is Flash in that sense?

    The Flash player might be installed on bazillions of browsers today, but there's no doubt that Silverlight has a lot more potential to win the hearts of millions of developers.

    Florian

    no comments
  •  ^
  • [WPFXAML]

    WPF DataBinding - was ist ein CurrentItem?

    by Florian Krüsch, September 17, 2007

    Als ich meine ersten Versuche mit Databinding in WPF machte (das war glaube ich so kurz vor Beta1), wunderte ich mich eine Zeit lang darüber, warum eine ListBox und ein ContentControl, beide an ein Array gebunden, schon eine funktionierende Master-Detail Ansicht ergaben.

    Der Magic Switch war offenbar die Eigentschaft IsSynchronizedWithCurrentItem, die man auf der ListBox auf True setzen muss. Damals gab es noch kaum Dokumentation und ich hatte die grosse Lernklippe noch vor mir.

    Das "Geheimnis" verbirgt sich hinter dem Interface ICollectionView. Die CollectionView sitzt zwischen den Controls und der gebundenen Collection (Array, List, DataTable etc). Sie merkt sich, welches Item gerade ausgewählt ist, erlaubt sortieren und gruppieren und reagiert mit dem Werfen von Events wenn Items z.B. hinzugefügt oder gelöscht werden.

    Die Eigenschaft IsSynchronizedWithCurrentItem sorgt nun dafür, dass das CurrentItem in der CollectionView immer mit dem Wert synchronisiert wird, den der User in der ListBox ausgewählt hat.

    Bindet man z.B. den Content eines ContentControl an eine Collection, so wird dort immer das CurrentItem dargestellt und fertig ist die Master-Detail View.

    Einen guten Überblick über die passenden Binding-Expressions gibt es bei Ian Griffith, dessen WPF Buch mir damals eine grosse Hilfe war. [http://www.interact-sw.co.uk/iangblog/2007/09/03/bindtocurrent]

    Hier noch ein paar Tips:
    • Über die Property Items kann man z.B. an einer ListBox direkt auf die CollectionView zugreifen.
    • Wenn man kein Navigieren benötigt, sondern nur eine Liste von Items darstellen will, nimmt man statt ListBox besser ItemsControl.
    • Um ein einzelnes Item über ein DataTemplate darzustellen, ist ContentControl eine gute Wahl.
    • Unabhängig von irgendwelchen Controls, kommt man per Code meist über CollectionViewSource.GetDefaultView an die passende CollectionView
    • Die Default-CollectionView ist Thread-spezifisch, also aufgepasst(!)

    Übrigens wird laut Scott Guthrie Silverlight in der Version 1.1 auch DataBinding unterstützen, genau wie das richtige WPF.

    Viel Spass,
    Florian

    no comments
  •  ^
  • [BlendXAML]

    Hacking Blend

    by Florian Krüsch, September 26, 2007

    If you're looking for ways to extend and customize Blend (and Cider) for your own needs, James Nakashima's blog is the one to watch.

    His trick on how to detect design mode (via Jaime) has been invaluable already on the handful of WPF projects I've been doing so far... Detecting design time can help you populate controls with suitable data or avoid execute code you don't need at designtime.

    The presentation James gave at TechEd is attached to this post. That looks pretty promising! You can even go so far and hook custom designers into Blend it seems.

    The things I've been playing around with so far is using TypeConverters and MarkupExtensions. TypeConverters can be useful if you want to help XAML or Blend to understand your custom types by converting back and forth to String.
    MarkupExtensions are powerful ways to hook your own services into XAML. Basically all that fancy curly bracket stuff in XAML like StaticResource/ DynamicResource, Binding Expressions or x:Static are markupextensions.

    One of the most powerful markupextensions I've come across is the EvalBinding extension in the blendables toolkit of IdentityMine. They're leveraging the JScript.NET compiler, which is a pretty neat idea... Leads me to an idea, how about using the IronRuby compiler there?

    If you want to get started writing your own markupextensions, take a look at the nice sample on the Windows SDK blog.

    cheers,
    Florian


    no comments
  •  ^
 

Language | Sprache

All[e]  |  english  |  deutsch
Tags
 

Profile

Florian Kruesch

I am working as a freelance software architect, developer and consultant in Düsseldorf, Germany.

My focus is on Microsoft technologies and .NET 3.0, especially WPF, ASP.net and SQL Server.

I've been a lead programmer on the WPF development of the OTTO Vista Store at SinnerSchrader Studios.

My client list includes DHL, LG and Ogilvy Interactive.