Wednesday, September 23, 2009

Handling Pessimism - Douchebag Coexistence

I tend to have what I call a douchebag coexistence problem; meaning when I perceive someone as a douchebag, all I seem to think about is their douchebagginess instead of listening to anything they have to say. They could just have created an awesome framework that builds enterprise applications in less than 10 lines of code AND makes me toast in the morning but the second they start talking to me this little voice in my head just starts repeating "douchebaggity doo douchebaggity doo...". Needless to say, this is not a healthy response to what is probably just a pessimistic person. It's something I'm working on.

So I read books like The No-Asshole Rule, Managing Humans and How to Win Friends and Influence People... and I still hear the voices. Honestly, the "How To Win Friends..." book is great for dealing with people in general and I recommend it to anyone who has to talk to people on a regular basis. Yes, anyone who has to talk to people in any capacity, at all, should read that book.

I also just read an interesting article on the Harvard Business Review website about How to Handle the Pessimist on Your Team. I recommend reading the full article, but I'll paraphrase shortly three approaches that are described:
1. Create awareness. This is best done by pulling the team member aside and explaining how his comments are received.

2. Reposition negative statements. Don't let negative comments linger; ask for clarification and maybe even require a follow up "But..." statement.

3. Involve the whole team. Set team norms and ask that everyone observe them.
So anyway, the first step is admitting you have a problem, right? Like I said, I'm working on it.

Now Playing - Jay-Z - Run This Town


Tuesday, September 15, 2009

DelegableEqualityComparer Dirtiness...


Occasionally I like to just get something done with some Lambda expressions that probably should be more properly architected.


In case someone else feels like doing something a little naughty with Lambda expressions, here is an example of what I call a DelegableEqualityComparer that will take as parameters the equality function to use and the function to generate a HashCode.


Disclaimer:  I didn't say it's the right way, just that it might be useful sometime.



    public class DelegableEqualityComparer<T> : IEqualityComparer<T> 

    { 

        private Func<T, T, bool> equalsFunc; 

        private Func<T, int> hashFunc; 

 

        public DelegableEqualityComparer(Func<T,T,bool> EqualityFunction, Func<T, int> HashFunction) 

        { 

            equalsFunc = EqualityFunction; 

            hashFunc = HashFunction; 

        } 

 

        #region IEqualityComparer<T> Members 

 

        public bool Equals(T x, T y) 

        { 

            return equalsFunc.Invoke(x, y); 

        } 

 

        public int GetHashCode(T obj) 

        { 

            return hashFunc.Invoke(obj); 

        } 

 

        #endregion 

    } 





// Example of inheriting for simple comparers. 

    public class ValidationResultMessageComparer : 

                 DelegableEqualityComparer<ValidationResult> 

    { 

        public ValidationResultMessageComparer() 

            : base( 

                // Equality Comparer 

                (x, y) => String.Equals(x.Message, y.Message), 

                // HashCode 

                (o) => o.Message.GetHashCode() 

            ) 

        { 

 

        } 

    } 





// Example of using in-line 

foreach ( var result in results.Distinct(new DelegableEqualityComparer<ValidationResult>((x,y) => x.Message.Equals(y.Message), o => o.Message.GetHashCode()))) 

{ 

        // Your distinct results... 

}




Now Playing: Rick Ross - Hustlin' Remix

Sunday, September 6, 2009

Bind HTML To Textblock with HTML to XAML Converter

I was recently looking for an easy way to get some existing HTML content resources into a WPF / Silverlight application. Now, in retrospect, it looks like I could have done something with a FlowDocument Viewer that might have been a little easier, but I figured I would post my solution in case it helps out someone else.

There are basically 3 pieces to binding HTML to a Textblock in WPF.
  1. Some HTML and a TextBlock to bind the HTML to.
  2. An HTML to XAML Converter (Provided by MSDN, adapted slightly)
  3. An Attached Property for the TextBlock.
The HTML to XAML Converter comes from a sort of proof of concept over on MSDN. I basically took the converter sample project and combined it all into one file so I could easily incorporate it into my project (or any other project for that matter; you can download the code below). The converter will parse the HTML, tokenize it, and convert it to a collection of Inline XAML elements that we can add to the TextBlock Inlines property.

All that's left is adding an Attached Property for us to bind to and replace the TextBlock's Inline elements. The way this works is to use a standard Attached Property and whenever our Attached Property changes, convert the bound HTML to a collection of Inline elements and add them to our TextBlock. The majority of the hard stuff is done by the HTML to XAML converter, but here is the code for the Attached Property and an example of the XAML to use it in your application.


Download Sample App / Code




// Attached Property to facilitate Binding HTMLto a TextBlock
    public static class HtmlTextBoxProperties 
    {         
        public static string GetHtmlText(TextBlock wb) 
        { 
            return wb.GetValue(HtmlTextProperty) as string; 
        } 
        public static void SetHtmlText(TextBlock wb, string html) 
        { 
            wb.SetValue(HtmlTextProperty, html); 
        } 
        public static readonly DependencyProperty HtmlTextProperty = 
            DependencyProperty.RegisterAttached("HtmlText", typeof(string), typeof(HtmlTextBoxProperties), new UIPropertyMetadata("", OnHtmlTextChanged)); 

        private static void OnHtmlTextChanged( 
            DependencyObject depObj, DependencyPropertyChangedEventArgs e) 
        { 
            // Go ahead and return out if we set the property
on something other than a textblock, or set a value that is not a string.
            var txtBox = depObj as TextBlock; 
            if (txtBox == null) 
                return; 
            if (!(e.NewValue is string)) 
                return; 
            var html = e.NewValue as string; 
            string xaml; 
            InlineCollection xamLines; 
            try 
            { 
                xaml = HtmlToXamlConverter.ConvertHtmlToXaml(html,
false);
                xamLines = ((Paragraph)((Section)System.Windows.Markup.XamlReader.Parse(xaml)).Blocks.FirstBlock).Inlines; 
            } 
            catch 
            { 
                // There was a problem parsing the html, return
out.
                return; 
            } 
            // Create a copy of the Inlines and add them to the
TextBlock.
            Inline[] newLines = new Inline[xamLines.Count]; 
            xamLines.CopyTo(newLines, 0); 
            txtBox.Inlines.Clear(); 
            foreach (var l in newLines) 
            { 
                txtBox.Inlines.Add(l); 
            } 
        } 
    } 




<TextBlock 
   x:Name="MyHtmlBlock"
   behaviors:HtmlTextBlockProperties.HtmlText="{Binding Path=MyHtml}"
   />




Now Playing - Matt & Kim - Daylight