Jaime Rodriguez
On Windows Store apps, Windows Phone, HTML and XAML

  • Jaime Rodriguez

    Forwarding the DataGrid’s DataContext to its’ columns..


    DataContext is a very handy inherited property on any WPF application..     
    Most of the time, I set DataContext near the root on the [logical] tree, and let the inherited DataContext do its magic to bind the rest of the scene.

    I recently tried to bind a DataGridColumn to its inherited DataContext (via its datagrid container) and got a very surprising answer on the output trace window:

    “System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element…”

    What is happening here?   
    The Columns collection is just a property in the Datagrid; this collection is not in the logical (or visual) tree, therefore the DataContext is not being inherited, which leads to there being nothing to bind to.

    I needed the functionality so I had to create a workaround.. With out much thought I decided to: 

    1. Listen for DataContextChanged in the DataGrid
    2. When DataContext changes,  forward the new value to the DataGridColumns in the datagrid.
    3. Bind properties on the DataGridColumn to this ‘forwarded’ DataContext  ( as I originally intended)

    To get it done,  I did not inherit from DataGrid and create a new class..  Instead i used the richness of WPF’s property system to pull a 1,2 punch:

    1. Override DataGrid’s DataContext metadata and listen for changes in it…
    2. Add a FrameworkElement.DataContextProperty to DataGridColumn …

    Code looks like this:


    FrameworkElement.DataContextProperty.OverrideMetadata ( typeof(DataGrid),            
    new FrameworkPropertyMetadata
    (null, FrameworkPropertyMetadataOptions.Inherits,
       new PropertyChangedCallback(OnDataContextChanged)));

    The OnDataContextChanged callback simply forwards the DataContext from DataGrid to its columns:

    public static void OnDataContextChanged ( DependencyObject d, DependencyPropertyChangedEventArgs e)
        DataGrid grid = d as DataGrid ; 
        if ( grid != null  ) 
            foreach ( DataGridColumn col in grid.Columns ) 
                col.SetValue ( FrameworkElement.DataContextProperty,  e.NewValue ); 

    That is it. Now we are ready to databind to DataGridColumns.

    <dg:DataGridTextColumn Binding="{BindingA}"
    Visibility="{Binding ElementName=ShowA,Path=IsChecked ,
    Converter={StaticResource BoolToVisConverter}}" />


    You can download source code for a small sample from here.

    The project has 3 checkboxes that are databound to a viewmodel…    The DataGrid’s column Visibility is databound to this same viewmodel,  if the checkboxes are checked ,the respective column is visible.. if unchecked, it is collapsed..

    A few more thoughts on DataGridColumn not being in the tree ..

    1. Binding to other UIElements via ElementName will not work because there is no tree.  
    2. Binding to a  StaticResource works fine..
    3. Binding to x:Static  will work fine too.

    Happy datagrid coding..  Again, there is source here.

  • Jaime Rodriguez

    Microsoft Client Continuum in action: The Silverlight toolkit charts, running in WPF


    The Silverlight toolkit CTP released at PDC includes some new charting functionality  (Column, Bar, Pie, Line, and Scatter).   
    The long-term plan is for all of these controls to be ported to WPF; but inspired by Rudi’s work on porting the themes, I peeked into the task to see if the Microsoft continuum would hold for the controls too.

    The results were darn good, I had the charts project compiled in WPF in ~20 mins; and after that, I only had to make a few run-time tweaks to get the project running… ( look for the #WPF  pre-processor in the code..   there is only a handful).

    Since the charting library is an early "preview" quality, I will probably not do a full port or any testing, but in case some one wants to carry it further, the source code is here..  

    Screenshot follows…  These are the same charts as in the SL Toolkit sample but running in a WPF app (named Window1 for Rob)..


  • Jaime Rodriguez

    Eight WPF themes released…


    Today, Rudi Grobler released a “WPF Themes Pack” on codeplex.
    The project includes eight XAML themes for WPF applications…

    Read his announcement here.   WPF Themes Codeplex project is here.

    I like his ‘Expression Dark’ theme.. It feels safe (and familiar) and I like the DavesGlossy theme too (screenshots below).

    Since some of the themes also ship with silverlight, this could be an easy way to create a consistent UI for an application that has a web client (Silverlight) and a destktop client (WPF).

    Thanks Rudi!!

    DavesGlossyControls ExpressionDark

  • Jaime Rodriguez

    NY Times ReaDER Free Edition..


    One of my favorite WPF apps is the NY times reader.. but a while ago they started charging a subscription fee and {since I am a cheap skate, and a very seldom commuter}  I stopped using it..

    Now they are back with a Free Edition..  It includes Top Stories,  Select Articles from Sunday Magazine, and Weekly crossword puzzle..   this is plenty for my 35 min once a week commute. It  will be good to keep up w/ the news to see who wins the US Presidential election (just kidding, I am not that out of touch –with the news at least) ..

    Install Times Reader Free Edition from here, and if you commute more often than I do, feel free to subscribe to full edition..


Page 1 of 1 (4 items)