The official Fatica Labs Blog! RSS 2.0
# Friday, August 12, 2011

Sometimes happens that we need to roll over all the combinations of elements in multiple arrays. This operation is called Cartesian Product and is defined as:

Definition (Cartesian product): Let A1, ..., An be n sets.

Then the set of all ordered n-tuples <x1, ..., xn> , where xi Ai for all i, 1 i n ,

is called the Cartesian product of A1, ..., An, and is denoted by A1 ... An .

Achieving this is possible by some linq tricks, or by an helper class I propose in this post. But just for clarify let’s start from the example:

We have three array as below:

 { "JUICY", "SWEET" }
{ "GREEN", "YELLOW" }
{ "APPLE", "BANANA", "MANGO" }

we want to obtain all the possible tuples, ie:

 

image

This can be achieved by the following code ( helper class ):

public class CartesianProduct<T>
    {
        int[] lengths;
        T[][] arrays;
        public CartesianProduct(params  T[][] arrays)
        {
            lengths = arrays.Select(k => k.Length).ToArray();
            if (lengths.Any(l => l == 0))
                throw new ArgumentException("Zero lenght array unhandled.");
            this.arrays = arrays;
        }
        public IEnumerable<T[]> Get()
        {
            int[] walk = new int[arrays.Length];
            int x = 0;
            yield return walk.Select(k => arrays[x++][k]).ToArray();
            while (Next(walk))
            {
                x = 0;
                yield return walk.Select(k => arrays[x++][k]).ToArray();
            }

        }
        private bool Next(int[] walk)
        {
            int whoIncrement = 0;
            while (whoIncrement < walk.Length)
            {
                if (walk[whoIncrement] < lengths[whoIncrement] - 1)
                {
                    walk[whoIncrement]++;
                    return true;
                }
                else
                {
                    walk[whoIncrement] = 0;
                    whoIncrement++;
                }
            }
            return false;
        }
    }

 

And, just for completeness, the example application:

 

static void Main(string[] args)
        {
            var cross = new CartesianProduct<string>(
               new string[] { "JUICY", "SWEET" }
               , new string[] { "GREEN", "YELLOW" }
               , new string[] { "APPLE", "BANANA", "MANGO" }
               );

            Console.WriteLine("=========================================================");
            foreach (var item in cross.Get())
            {
                Console.WriteLine("{0}\t{1}\t{2}", item[0], item[1], item[2]);
            }
            Console.WriteLine("=========================================================");
        }
Really simple and clear to use in comparison with other linq based solution, even when arrays are unknown at design time.
Friday, August 12, 2011 11:46:46 AM (GMT Daylight Time, UTC+01:00)  #    Comments [1] - Trackback
Programmin | Recipes

# Wednesday, August 10, 2011

Sometimes the difference between unit and integration test does not apply. Maybe because writing a mock is too complex or boring, or maybe you have an integration test already working and you want to use it as a unit test but the integration isn’t always available or it is slow and so on … A possible solution is “Parroting” the integrated part by creating automatically a mock when running the integration tests. A friend of mine, just realized such an idea: Wasp Parrots, even if at an early stage, it looks very promising.

Wednesday, August 10, 2011 10:45:40 AM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback


# Tuesday, August 09, 2011

Sometimes it is annoying that the TextBox in WPF needs to loose focus in order to updating the underlying value in the model. Having the application moving the focus in order to force the binding update is a bad trick, unacceptable if you are using MVVM. In such a case worked for me deriving a custom class from the textbox ( actually I did for the Fluent Ribbon textbox, but the concept is the same ) and programmatically updating the source, as below:

public class UpdatingBindingTextBox:TextBox
    {
        public UpdatingBindingTextBox()
        {
            this.TextChanged += new System.Windows.Controls.TextChangedEventHandler(UpdatingBindingTextBox_TextChanged);
        }

        void UpdatingBindingTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            BindingOperations.GetBindingExpression(this, TextBox.TextProperty).UpdateSource();
        }
    }
So this force the model to update at each character in input.

ADDITION:

By investigating a little more, I found this alternative on stack overflow. It is basically a behavior attached to the textbox that call a command in the ViewModel. Don’t know which one is better, I personally like the behavior way, but in any case I do need to update the binding manually into the view model…

Tuesday, August 09, 2011 4:16:06 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
WPF

# Thursday, July 21, 2011

If you plan to use the Caliburn Micro convention with buttons contained in a Fluent Ribbon you will notice that it does not works out of the box. This is because the default BindingScope.GetNamedElements does not dig inside the ribbon to looks for named control. The solution that worked for me is to modify the default behavior as swown below:

In the overridden Configure function:

defaultElementLookup = BindingScope.GetNamedElements;
           BindingScope.GetNamedElements = new Func<System.Windows.DependencyObject, IEnumerable<System.Windows.FrameworkElement>>(
               k =>
               {
                   List<FrameworkElement> namedElements = new List<FrameworkElement>();
                   namedElements.AddRange(defaultElementLookup(k));
                   Fluent.Ribbon ribbon = LookForRibbon(k);
                   if( null != ribbon )
                       AppendRibbonNamedItem(ribbon, namedElements);
                   return namedElements;
               }
               );

We need to define defaultElementLookup into the boot class as:

Func<DependencyObject, IEnumerable<FrameworkElement>> defaultElementLookup;
I saved the standard GetNamedElement function because I use it to look for the control the default way, and to look for the controls inside the Ribbon too.

Here the function LookForRibbon, that just dig into the visual tree to search for a ribbbon ( if any ):

private Fluent.Ribbon LookForRibbon(DependencyObject k)
        {
            Fluent.Ribbon foundRibbon = null;
            var contentControl = k as ContentControl;
            if (null != contentControl)
            {
                var child = contentControl.Content as DependencyObject;
                if( null != child )
                    return LookForRibbon(child);
            }
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(k); ++i)
            {
                var child = VisualTreeHelper.GetChild(k, i);
                foundRibbon = child as Fluent.Ribbon;
                if (null != foundRibbon)
                {
                    return foundRibbon;
                }
                else
                {
                    foundRibbon = LookForRibbon(child);
                    if (null != foundRibbon)
                        return foundRibbon;
                }
            }
            return null;
                
        }

 

As you guess, the function stops at the first found ribbon so we suppose there is just one ribbon for view ( quite reasonable )

As we have the ribbon we can look for the elements inside and append to the list with this function:

 

private void AppendRibbonNamedItem(Fluent.Ribbon ribbon, List<FrameworkElement> namedElements)
        {
            foreach (var ti in ribbon.Tabs)
            {
                foreach (var group in ti.Groups)
                {
                    namedElements.AddRange(defaultElementLookup(group));
                }
            }
        }

 

So we look inside each ribbon group and we treat these as Caliburn would do.

Thursday, July 21, 2011 2:35:52 PM (GMT Daylight Time, UTC+01:00)  #    Comments [1] - Trackback
Caliburn | WPF

# Tuesday, June 07, 2011

Sacha Barber expand his work on a very famous project presented about 4 years ago on CodeProject. It is a very interesting project about class diagram creation in WPF, mainly because is a good field for learning advanced WPF functionality. Here Sacha explain the new version, and below a screen shot:

Tuesday, June 07, 2011 8:22:55 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
WPF

Just add a new feature to decide where to place the vertical axis: left or right aligned to the plot area. Here the property usage:

<ddd:ChartPlotter MainVerticalAxisPlacement="Right"  >

 

A modification was necessary even in the WidthSpring element, the one that allow us to keep more than one chart synchronized on the X axis ignoring the space taken by the label area. It is now possible to specify that the WidthSpring must act on the left of the chart. Here the usage with the new properties:

<ddd:WidthSpring ActiveOnSide="Right" SourcePanel="{Binding RightPanel, ElementName=volume}"/>

 

by this addition the chart behave correctly even with the right side placed vertical axis:

r4

Source for this project can be found here.

Tuesday, June 07, 2011 8:15:50 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
D3 | WPF

My Stack Overflow
Contacts

Send mail to the author(s) E-mail

Tags
profile for Felice Pollano at Stack Overflow, Q&A for professional and enthusiast programmers
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2014
Felice Pollano
Sign In
Statistics
Total Posts: 157
This Year: 0
This Month: 0
This Week: 0
Comments: 123
This blog visits
All Content © 2014, Felice Pollano
DasBlog theme 'Business' created by Christoph De Baene (delarou) and modified by Felice Pollano