Wednesday, April 13, 2016

Need to Synchronize Columns of a Master/Detail Grid in DevEx WPF?

I generally prefer Telerik controls, but I've got a client that uses Developer Express.  I recently had a need to synchronize columns in a Developer Express WPF Master/Detail grid.   It's a bit of an unusual circumstance, where we have Master / Detail records that use the same view interface, but found the TreeListControl unable to scale up to the demands of our use cases.  The client still wanted the detail grid's columns to appear to functionally be the same column as the master record's (with the ability to show/hide the detail).

Thankfully, the Dev Express GridColumn class is a DependencyObject, and all the needed properties are exposed as DependencyProperty's.

Since the detail grid has the same data interface as the master grid, I was even able to clone the column definitions. 

Finally, since this was an MVVM project, I didn't want the functionality in code-behind, so I abstracted the code for this into a Behavior.

The approach was to bind the Width, Visibility, and VisibleIndex properties of each master grid column to a cloned detail grid column, giving the two entities the appearance of being one functional entity.
Here's the snippet representing the detail grid definition....


<!-- Master grid defined here, not shown.  Detail grid below   -->


                <dxg:DataControlDetailDescriptor ItemsSourceBinding="{Binding Details}">

                    <dxg:GridControl x:Name="DetailsGrid" AutoGenerateColumns="None" ColumnsSource="{StaticResource ColumnsCollection}" >

                        <dxg:GridControl.View    >

                            <dxg:TableView  AutoWidth="False"




                            CellStyle="{StaticResource DefaultCellStyle}"









                            RowStyle="{StaticResource RowStyle}"

                            AlternateRowBackground="{x:Static dxRes:DevExpressResources.AlternateRowBackgroundBrush}"












Note the highlighted part above that introduces a local class called SyncDetailGridColumsBehavior, shown in its entirety below:

using System.Windows.Data;

using System.Windows.Interactivity;

using DevExpress.Xpf.Grid;

namespace Local
    public class SyncDetailGridColumnsBehavior : Behavior<GridControl>
        private GridColumnCollection _parentGridColumns;
        private GridColumnCollection _detailsGridColumns;

        protected override void OnAttached()
            _detailsGridColumns = AssociatedObject.Columns;
            _parentGridColumns = AssociatedObject.ResolveParentColumnCollection();
        private void InitializeMasterDetailGrid()

    internal static class ColumnHelpers

        public static GridColumnCollection ResolveParentColumnCollection(this GridControl associatedObject)
            var result =
                    ((System.Windows.FrameworkContentElement) associatedObject.Parent).Parent).Columns;
            return result;

        public static void CloneColumnsAndBindWidthsFrom(this GridColumnCollection targetGridColumns,
            GridColumnCollection sourceGrid)
            foreach (var aSourceColumn in sourceGrid)
                var aClonedColumn = aSourceColumn.Clone();

        public static GridColumn Clone(this GridColumn source)
            return new GridColumn()
                Name = source.Name,
                Width = source.Width,
                Binding = source.Binding,
                Header = source.Header,
                Style = source.Style,
                CellStyle = source.CellStyle,
                CellTemplateSelector = source.CellTemplateSelector,
                CellTemplate = source.CellTemplate,

        public static void BindWidths(this GridColumn source, GridColumn bindingPartner)
                new Binding("ActualWidth")
                    Source = bindingPartner,
                    Mode = BindingMode.OneWay,
                    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                new Binding("ActualWidth")
                    Source = source,
                    Mode = BindingMode.OneWay,
                    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged

        public static void BindPositions(this GridColumn source, GridColumn bindingPartner)
                new Binding("VisibleIndex")
                    Source = bindingPartner,
                    Mode = BindingMode.TwoWay,
                    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged

        public static void BindVisibility(this GridColumn source, GridColumn bindingPartner)
                new Binding("Visible")
                    Source = bindingPartner,
                    Mode = BindingMode.TwoWay,
                    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged


I expect this to cover 90% of our needs, the other 10% has to do with row selection across master/detail boundaries, but that's a story for another day.

In the meantime, let me know how this makes ya feel...  leave a comment, below.


Sunday, April 10, 2016

Rise of the Smart App

Microsoft didn't talk much about the Windows Phone at Build 2016.  If you think that's news, you're missing the point.

As Microsoft re-defines "Mobile First, Cloud First" they declare shenanigans on the idea that the tech world revolves around phone and tablet.  Yes, tablet and smartphone are mature, first-class citizens, now, but they're not above laptops, PCs, or other computing devices, as Apple (and perhaps even Samsung) might have you believe.

There's no denying that Microsoft lost the battle for smartphone market share.  RIM's Blackberry, considered a relic of the primordial smartphone market, is all but forgotten. Microsoft was pushing Windows Phone as significant competitor, yet, with about the same market share as Blackberry, no one really took their smartphone offering seriously. 

Until Windows Phone's recent convergence with the PC on the universal Windows 10 OS, Windows Phone had no more competitive edge than Blackberry, either.  Sadly, this new competitive edge comes too little, too late. Or has it?

Several years ago, in a very sly move, Apple narrowed and laser-focused the global technology mindset on a much smaller battle... one that it was well positioned in. Apple then equated the battle to the war... They made it all about the smartphone/tablet market.  (I don't think Apple counted on Android, but it didn't matter... in terms of market share, Android won, but in terms of profitability, Apple won.)  Billions of dollars can't be wrong, so Microsoft tried to position itself in Apple's vision, and let itself get dragged around for years... 

Until now.

By connecting Mobility with Portability, Microsoft is driving the scope of technology mindshare again, and are driving it back out to a scale Apple will have to struggle to position itself in. Apple made good smartphones.  Cool beans.

With its converged "Universal" Windows 10 platform, Xamarin portability, and mature cloud offerings replete with machine learning, Microsoft is targeting a much broader "smart app" market... Smart Apps are apps that make any device (keyboard, mouse, display/touchscreen, microphone, pen, scanner, camera, video recorder/editor, audio mixer, cell phone, media player, whiteboard, virtual/augmented reality, what have you) into a smart device.  (Notice anything missing here?  perhaps cars...  but it's hard to imagine that won't change in the next few years...  after all, cars (e.g. BMW) did get mentioned at Build.) 

The smartphone isn't irrelevant, it's just not the whole pie. The reality is that Microsoft is not going to exclude phones from Windows 10 now or any time soon. 

Smartphone prominence is not innovation superiority.

So, how does this make you feel?

Thursday, March 31, 2016

Mobile First, Cloud First as Redefined in Build 2016

There were a number of very cool announcements made at Microsoft Build's 2016 kickoff today, Wednesday, March 30th.  On first brush, one might not notice the common theme across the announcements, however.
These things may seem disconnected, but if you look again, they're not... nor are the other less obviously connected major announcements such as:
Microsoft has been promising Mobile First, Cloud First for a long time. Until today, there's been a consensus that Mobile First meant Tablet and Phone...  as in those hardware form factors get updates and features before classic alternatives.  Today Satya Nadella, CEO of Microsoft, addressed the developer crowd and clarified (or perhaps more accurately "redefined") what "Mobile First" really means.

Mobile First, Cloud First has morphed to mean something more like "Portable First, Cloud First", with portable referring directly to the Universal Windows Platform...  write your code once, and the user can take it to any device (desktop, laptop, phone, IoT) and use it as naturally as possible with the whatever human interface device is available (keyboard, mouse, touch, ink, voice, text, etc)...

And there is, indeed, how all the announcements fit together. 

All of the announcements made today talk about how Microsoft is diversifying the human interface, yet keeping the context of all your work and play available across all devices.  Your apps are there, and they work just as naturally with voice commands as they do with ink. 

Note that keyboard and mouse are relatively unnatural compromises in human interface paradigm, and their use will be diminished in the future. 

If pen and paper is the natural choice, Ink will replace it. 

If voice makes sense, it will be available.

If an intelligent actor might assist you, Cortana and your trusted "Network of Experts" (Apps and BotFX bots) are there. If a virtual or augmented reality makes sense, your apps will join you on Hololens. 

If an Xbox controller is the right choice, no problem. 

All of these things can be enhanced significantly with DirectX 12, even for non-games.

And the Cloud will interlink them naturally so that you can flip from tablet to Hololens without skipping a beat. (in some ways, live process migration is really becoming a reality;  when the apps are the same across every deployed device, the only thing that needs to transfer from device to device is user data, and that's happening via cloud.)

One segment that didn't see much love today:  the Web.  Or did it?  Bash for Windows 10 is a bit of a stretch to connect to the coming shift, but really, it is meant to make it easier for developers to deliver web-based, cloud-hosted solutions using Visual Studio to non-Windows hosts.  Indirectly this will aid in the creation of services that might support Bots made with the BotFX, for example.  All of this is based predominantly on JSON over REST / HTTP.

Some would point out that Windows 10 Mobile (formerly Windows Phone) didn't get any stage time, either, but the reality is that Microsoft no longer sees the smartphone edition of Windows to be a separate thing.   Windows 10 is converging on "The Best Windows Ever", Windows 10... smartphone or not. 

Sunday, March 20, 2016

Tribute to the TI 80-something Graphing Calculators

Rummaging thru a box of stuff in storage, I ran across my old Texas Instruments TI-85 calculator.  I had to stop and fiddle it for a moment. I grabbed some AAA batteries, only to discover that the CR1616 backup had given up... the calculator operates like new, as in factor reset... but sadly, that means a game program I wrote for it 20 years ago was finally gone forever.

Back in the early 90's, Exeter Area High School had an advanced math course that required a TI-81 calculator. I gladly used the course as an excuse to get my hands on this relatively expensive (near $100 in 1990 dollars) but amazing piece of hardware at the time.
In short order, I added its programming language to the list of languages I had already taught myself.  I loved using trigonometric functions to create pictures. I used to program it to do my trig and pre-calc homework for me. (In retrospect, my attempt at "cheating" was a hack that I learned more from than any lecture or textbook ever would teach me... you see, in order to program the calculator to do advanced math for me, I had to thoroughly understand it, myself.)

My own TI-85, which I upgraded to
for calculus at UNH. It's a bit dusty today.
It cost about $100 when I
purchased it in the early 90's.
The TI-8x calculators were my "gateway drug" to my love of mobile development.  I was already developing software for PCs, but I loved the challenges imposed by yet more limited footprints and hardware capabilities. 

I kept my TI with me to the point that my sister nicknamed it a "porta-geek"...  (There was even a girl who stole it from me, thinking to wound me for the fact that I wasn't interested in dating her. It had no effect; I obliviously assumed I'd misplaced it in my own absent-mindedness. The story came out several years later, after I'd purchased a replacement.)

The term "porta-geek" is a term I still whimsically apply on occasion to my current daily driver mobile device, my Lumia 950XL running Windows 10.  My sister's jibe didn't phase me, either.

By coincidence, I was also in Target's electronics department today. I was surprised to notice that they still had several 20+ year old TI-80-somethings...  but rather than the price being lower, the prices are actually higher.

A selection of same-generation TI calculators at Target today (3/20/2016).  Notice the TI-80-somethings still going for about $100+.

I can't really say why 20+ year old calculators should still cost more than they originally did...  clearly normal technology market forces are not in effect for them.  My spidey-sense for socialistic-driven monopoly tingles.  I'm not the only one to have noticed the... discrepancy... over the past couple decades.  ( )

That said, they were, and are great devices.

I'd love to see a Windows 10 emulator app made out of them....  maybe some day I'll find enough spare time...  :)

Friday, February 26, 2016

Intro to Rx.NET (Reactive Extensions)

Thanks to the gang for joining me at the Microsoft Store in Salem NH for my preso on "Intro to Rx.NET"   Being that it's a toolkit I've been digging a lot at work lately, I had a feeling folks might appreciate a broad brush into to it.

Please check out the Granite State (NH) Windows Platform App Devs (#WPDevNH) on to connect with the group and maybe even participate, yourself.  In addition to the core presentation topic, we had a great debate in speculation on how Microsoft's purchase of Xamarin might settle out.  Also, I'll be attending Build 2016, so we're talking about having a special meeting early in April to recap and consider future presentations. (stay tuned!)

Rx reminds me a lot of other declarative language elements (XSL, XAML) in that it seems really natural, then you start looking at more advanced stuff and the complexity becomes boggling... then you start to really understand the abstractions and it feels natural again.

Without further ado, here's my slides for the presentation:

I'd like to thank the folks at, I referenced them more than any other source putting this together.

Finally, for the code I demoed, please check out the post I mentioned, here:

Hope to see you soon!
-Jim Wilcox
The Granite State Hacker

Wednesday, January 20, 2016

Rate Limiting Events with the Reactive Extensions Rx

A fun little challenge that came up at work...  we have a stream of events that we publish / subscribe via a Reactive IObservable.   One of the things we're testing for is swamping the subscribers with events for periods of time.  The tools in System.Reactive.Linq namespace include utilities like .Throttle(...), .Sample(...).  None of these supported our needs.

For our needs in this particular event stream, in this point in our stream, we can't afford to drop events.  

Sample(...) picks off an item at various intervals, dropping the rest.  Throttle(...) sets up a window of time. It won't release any object until there's been a case where only one object was buffered in the given time window.  If you get another while the window's open, the window widens to the original timespan.

Then there's .Buffer(...) which can store event objects for a window, and then release them.  That amounts to releasing all the events in periodic bursts, but it's not a rate limit.

Finally there's the .Delay(...) method... which, ironically, delays publishing objects by an offset amount...  but that delays all objects by the same time offset.  If you have three events come in, 1 millisecond apart each, and put a 1 minute delay on them, they'll enter the collection, and one minute later, will be published out... in a three-millisecond burst.

I want to be able to constrain my publisher such that I only want n number of entities per second. 

My solution separates the pub/sub.  It loads a queue with incoming events, and emits them immediately, up to the limit on the subscriber side. On the publisher side, it resets the counter and emits any overflow objects in the queue, also up to the limit.   

Yes, this model has problems, so address your risks appropriately... ("use at your own risk").    You can run out of memory if the origin provides more items than the limiter is allowed to emit over long periods of time.

Anyway, here's a Program.cs with the RxHelper extension RateLimit(...).  The program has a decent little before/after RateLimit(...).

using System;
using System.Collections.Concurrent;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;

using System.Reactive.Linq;
namespace ConsoleApplication2
    class Program
        static void Main(string[] args)
            // simulate very fast (no delay) data feed
            var unThrottledFeed = GetFastObservableSequence();

            Console.WriteLine("That was an example of an event stream (with 100 events) only constrained by system resources.");


            Console.WriteLine("Now rate-limiting at 10 items per second...\n");

            const int itemsPerSecond = 10;
            var throttledFeed = GetFastObservableSequence()
            Console.WriteLine("END OF LINE");
            Console.WriteLine("Note that the Main() method would be done here, were it not for the ReadKey(), the RateLimit subscriber is scheduled.");
            Console.WriteLine("Rate limited events will appear here:");
        #region Example Artifacts
        private static IObservable<TestClass> GetFastObservableSequence()
            var counter = 0;
            var rnd = new Random();
            return Observable.Defer(() =>
                    .Select(p =>;
                        var x = new TestClass(30.0, counter);
                        x.Value += Math.Round(rnd.NextDouble(), 2);
                        return x;
        private class TestClass
            public TestClass(double value, int instance)
                Value = value;
                Instance = instance;
            private int Instance { get; set; }
            public double Value { get; set; }
            public override string ToString()
                return $"{Instance}: {Value}";
    internal static class RxHelper
        public static IObservable<TSource> RateLimit<TSource>(
            this IObservable<TSource> source,
            int itemsPerSecond,
            IScheduler scheduler = null)
            scheduler = scheduler ?? Scheduler.Default;
            var timeSpan = TimeSpan.FromSeconds(1);
            var itemsEmitted = 0L;
            return Observable.Create<TSource>(
                observer =>
                    var buffer = new ConcurrentQueue<TSource>();
                    Action emit = delegate()
                        while (Interlocked.Read(ref itemsEmitted) < itemsPerSecond)
                            TSource item;
                            if (!buffer.TryDequeue(out item))
                            Interlocked.Increment(ref itemsEmitted);

                    var sourceSub = source
                        .Subscribe(x =>

                    var timer = Observable.Interval(timeSpan, scheduler)
                        .Subscribe(x =>
                            Interlocked.Exchange(ref itemsEmitted, 0);
                        }, observer.OnError, observer.OnCompleted);
                    return new CompositeDisposable(sourceSub, timer);


Edit 1/27/2016:  Had to tweak RateLimiter(...) to immediately emit objects as long as it hadn't hit it's limit for the time span.  It always queues, just in case, to maintain order.