Archive for December 2012

Measure twice, refactor once

Just a quick year end post for my loyal readers…

When you are looking to make performance improvements on your iOS app, no matter how well you think you know the code, fire up Instruments and run your code on a real device. Before the holiday break, I was not going to do this because I was certain where the code was slowing down, but on a lark I decided to profile it and needless to say it was a real eye opener. I saved myself a bundle of time by not chasing down phantom slow downs, made the code run super super fast, and got the girl in the end. (OK, maybe not that last one.)

Happy and prosperous New Year to one and all.

Overriding a TargetType Style without a Key in WPF

I have been doing a lot of work in WPF lately, and it is a different animal.

The default styles of a button did not look right for my application, so I came up with the following XAML that styles up all of the button objects in my application:

<Style TargetType="{x:Type Button}">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="Background" Value="CornflowerBlue" />
    <Setter Property="Template">
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="Border" CornerRadius="0" BorderThickness="0" 
                            Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding Background}">
                    <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" />
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="Border" Property="Background" Value="#4667A5" />
                    <Trigger Property="IsPressed" Value="true">
                        <Setter TargetName="Border" Property="Background" Value="#3C588C" />
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter TargetName="Border" Property="Background" Value="LightGray" />
                        <Setter TargetName="Border" Property="BorderBrush" Value="#AAAAAA" />
                        <Setter Property="Foreground" Value="DarkGray"/>
                    <Trigger Property="IsEnabled" Value="true">
                        <Setter Property="Foreground" Value="White"/>

As you can see, I am using TemplateBinding to bind to the desired background color.

However, in one of my user controls, I want to be able to change the background color of the button in certain situations. Initially, I just created a copy of the above XAML, gave it a Key, and then used that key name to style up the Buttons that I wanted to.

As always, there is a better way. I found that there was a way to use a BasedOn in my new style to pull in the style from the global resources and just change what I needed, here is what it looks like:

<Style x:Key="SpecialButton" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
    <Setter Property="Width" Value="20" />
    <Setter Property="Visibility" Value="Collapsed" />
    <Setter Property="Content" Value=">" />
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="Background" Value="LightGray" />

BTW, Happy Birthday to Eugene Levy, by far one of the funniest actors out there.

A prediction by the USPS???

When I was looking through my father’s collection of U.S. Postal Service First Day Covers, I found this gem. You would think that our postal service would have been more vocal about this in the past 38 years or so.

BTW, Happy Birthday to Charlie Batch, engineer of an improbable win for the Pittsburgh Steelers over the Baltimore Ravens this past weekend. It was nice to see an old guy like me doing something awesome.